我尝试使用pp(Parallel Python):
import glob
import subprocess
import pp
def run(cmd):
print cmd
subprocess.call(cmd, shell=True)
job_server = pp.Server()
job_server.set_ncpus(8)
jobs = []
for a_file in glob.glob("./*"):
cmd = "ls"
jobs.append(job_server.submit(run, (cmd,)))
for j in jobs:
j()
但是遇到了subprocess.call不是全局名称的错误。
An error has occured during the function execution
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pp-1.6.1-py2.7.egg/ppworker.py", line 90, in run
__result = __f(*__args)
File "<string>", line 3, in run
NameError: global name 'subprocess' is not defined
我已经导入了子进程,为什么不能在这里使用它?
根据abarnert的建议,我将代码更改为:
import glob
import pp
def run(cmd):
print cmd
subprocess.call(cmd, shell=True)
job_server = pp.Server()
job_server.set_ncpus(8)
jobs = []
for a_file in glob.glob("./*"):
cmd = "ls"
jobs.append(job_server.submit(run, (cmd,),modules=("subprocess",)))
for j in jobs:
j()
但它仍然不起作用,它抱怨这样:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
self.run()
File "/usr/lib/python2.6/threading.py", line 484, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/local/lib/python2.6/dist-packages/pp-1.6.1-py2.6.egg/pp.py", line 721, in _run_local
job.finalize(sresult)
UnboundLocalError: local variable 'sresult' referenced before assignment
答案 0 :(得分:2)
文档很好地解释了这一点,每个例子都向您展示了如何处理它。
提交方法的参数之一是“modules - 要导入的模块名称的元组”。您希望在提交的作业中可用的任何模块都必须在此处列出。
所以,你可以这样做:
jobs.append(job_server.submit(run, (cmd,), (), ('subprocess',)))
或者这个:
jobs.append(job_server.submit(run, (cmd,), modules=('subprocess',)))
答案 1 :(得分:1)
抱歉,未经测试,但您是否尝试过:
from subprocess import call
在'run'函数中?
然后使用“call”而不是“subprocess.call”?这将使“调用”功能本地化,但可以访问。