我有 2 个文件 main.py 和 infinit.py,如下所示:
主文件
#!/usr/bin/python
import logging
import subprocess
import sys
logging.basicConfig(level=logging.INFO)
def forever():
cmd = [sys.executable, 'infinit.py']
while 1:
try:
print 'running new instance of:'
print ' '.join(cmd)
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, universal_newlines=True)
for line in iter(popen.stderr.readline, ""):
print line,
for line in iter(popen.stdout.readline, ""):
print line,
except Exception as e:
print 'Something bad happend'
logging.error(e)
if __name__ == '__main__':
forever()
无限.py
#!/usr/bin/python
import logging
logging.basicConfig(level=logging.INFO)
i = 0
while 1:
i += 1
logging.info('i: {0}'.format(i))
print i
我运行 main.py 并且我想在我的控制台中同时查看(打印和日志记录)。我也希望它在 windows 和 linux 上运行。此外,它是否可以在 Windows Idle 中工作(打印和记录)?