编辑2
不要使用
os.system()
使用
subprocess.call()
system("%s --dna %s --multifasta %s --target %s -o %s -q >>%s" %(abfgp_cmd, osjoin(inbase, id_, id_ +".dna.fa"), osjoin(inbase, id_, "informants.mfa"), id_, out, log))
将转换为
subprocess.call((abfgp_cmd, '--dna', osjoin(inbase, id_, id_ +".dna.fa"), '--multifasta', osjoin(inbase, id_, "informants.mfa"), '--target', id_, '-o', out, '-q')) # without log.
编辑1
我认为问题是多处理使用模块名来序列化函数和类。
这意味着如果你
import module
模块所在位置
./module.py
你做的事情
os.chdir('./dir')
现在你需要
from .. import module
.
子进程继承父进程的文件夹。这可能是个问题。
解决
-
确保导入所有模块(在子进程中),然后更改目录
-
插入原件
os.getcwd()
到
sys.path
以启用从原始目录导入。这必须在从本地目录调用任何函数之前完成。
-
将您使用的所有函数放在始终可以导入的目录中。这个
site-packages
可能是这样的目录。然后你可以做一些类似的事情
导入模块
module.main()
开始你的工作。
-
这是我做的一个黑客,因为我知道泡菜是如何工作的。仅当其他尝试失败时使用此选项。
脚本打印:
serialized # the function runD is serialized
string executed # before the function is loaded the code is executed
loaded # now the function run is deserialized
run # run is called
在你的情况下,你会这样做:
runD = evalBeforeDeserialize('__import__("sys").path.append({})'.format(repr(os.getcwd())), run)
p = Process(target=runD, args=(Q, pid+1))
这是脚本:
# functions that you need
class R(object):
def __init__(self, call, *args):
self.ret = (call, args)
def __reduce__(self):
return self.ret
def __call__(self, *args, **kw):
raise NotImplementedError('this should never be called')
class evalBeforeDeserialize(object):
def __init__(self, string, function):
self.function = function
self.string = string
def __reduce__(self):
return R(getattr, tuple, '__getitem__'), \
((R(eval, self.string), self.function), -1)
# code to show how it works
def printing():
print('string executed')
def run():
print('run')
runD = evalBeforeDeserialize('__import__("__main__").printing()', run)
import pickle
s = pickle.dumps(runD)
print('serialized')
run2 = pickle.loads(s)
print('loaded')
run2()
如果这些不起作用,请反馈。