我使用ipython parallel来使用load_balanced_view
安排大量工作。每个作业使用subprocess.Popen
来运行代码并检索stdout和stderr。然后我想将它们保存到日志文件中。
这是我正在运行的代码结构的一个示例:
import subprocess as sp
def make_inifile(c):
...
def run_exe(exe, inifile):
def_Popen_kwargs = {'stdout': sp.PIPE, 'stderr': sp.PIPE,
'universal_newlines': True}
pexe = sp.Popen([exe, inifile], **def_Popen_kwargs)
(stdout, stderr) = pexe.communicate()
with open('logfile.log', 'a') as f:
f.write(stdout)
f.write(stderr)
rc = Client()
lbv = rc.load_balanced_view()
rc[:].execute("import subprocess as sp", block=True)
exe = "/path/to/exe"
for c in cases:
inifile = make_inifile(c)
lbv.apply(exe, inifile)
lbv.wait()
由于我将使用多个处理器,因此在最好的情况下,日志文件看起来就像一团糟。 解决方案可能是锁定文件,这样一次只有一个进程可以写入它。这应该是可行的,但对我来说看起来有点矫枉过正。
更好的解决方案可能是使用文件名中的引擎ID为每个引擎打开日志文件。这样的事情:"logfile_{}.log".format(id)
所以问题是:有没有办法从run_exe
内检索引擎ID?
答案 0 :(得分:1)
使用dview在启动时将客户端ID推送到客户端,还是使用os.getpid()
?