【发布时间】:2016-08-08 22:59:51
【问题描述】:
我正在尝试使用ipyparallel 并行化来自here 的一些代码。简而言之,我可以使函数在 apply_sync() 之外正常工作,但我似乎无法让它们在其中工作(我发誓我之前有这个工作,但我找不到代码的版本没有坏)。一个简单的例子:
def test3(fname = '7_1197_.txt'):
import subprocess
command = 'touch data/sentiment/' + fname + '.test'
child = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
while True:
out = child.stdout.read(1)
if out == '' and child.poll() != None:
return
test3() #this works, creates a file with the .test extention
results = view.map_sync(test3, speeches) #this doesn't work. No files created.
这是我实际要使用的函数的简短版本。它自己工作得很好。在apply_sync() 中,它根据htop 启动java 进程,但它似乎没有从这些进程中得到任何回报。
def test2(fname = '7_1197_.txt'):
import subprocess
settings = ' -mx5g edu.stanford.nlp.sentiment.SentimentPipeline'
inputFile = ' -file data/sentiment/' + fname
command = 'java ' + settings + inputFile
child = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
results = []
while True:
out = child.stdout.read(1)
if out == '' and child.poll() != None:
return ''.join(results)
if out != '':
results.extend(out)
test2() #Works fine, produces output
results = view.map_sync(test2, speeches) #Doesn't work: the results are empty strings.
我尝试了一个返回命令变量的版本。发送到Popen 的命令很好,并且在命令行中手动粘贴时它们可以工作。我想这可能只是管道的问题,但是更改命令以将输出重定向到带有' > '+fname+'.out' 的文件在apply_sync() 调用中也不起作用(不生成输出文件)。
我应该怎么做才能从系统回调中获得stdout?
【问题讨论】:
标签: python-2.7 subprocess stanford-nlp ipython-parallel