I am using a commercial application called Abaqus/CAE1 with a built-in Python 2.6 interpreter and API. I've developed a long-running script that I'm attempting to split into simultaneous, independent tasks using Python's multiprocessing
module. However, once spawned the processes just hang.
该脚本本身使用仅通过 Abaqus 专有的可用的各种对象/方法cae
模块,只能通过首先启动与 Abaqus/CAE 捆绑的 Python 来加载,然后使用 Python 执行我的脚本execfile
.
To try to get multiprocessing working, I've attempted to run a script that avoids accessing any Abaqus objects, and instead just performs a calculation and prints the result to file2. This way, I can run the same script from the regular system Python installation as well as from the Python bundled with Abaqus.
当使用以下任一命令从命令行运行时,下面的示例代码可以按预期工作:
C:\some\path>python multi.py # <-- Using system Python
C:\some\path>abaqus python multi.py # <-- Using Python bundled with Abaqus
这会产生新的进程,每个进程都会运行该函数并将结果按预期写入文件。但是,当从 Abaqus/CAE Python 环境调用时,使用:
abaqus cae noGUI=multi.py
然后 Abaqus 将启动,自动导入其自己的专有模块,然后使用以下命令执行我的文件:
execfile("multi.py", __main__.__dict__)
其中全局命名空间 arg__main__.__dict__
由 Abaqus 设置。然后,Abaqus 成功检查每个进程的许可证,生成新进程,然后......就是这样。进程已创建,但它们都挂起且不执行任何操作。没有错误消息。
可能是什么原因导致挂起?如何解决?是否有必须设置的环境变量?是否有其他使用类似程序的商业系统可供我学习/模仿?
请注意,任何解决方案都必须在Python 2.6 https://docs.python.org/release/2.6.9/library/index.html#the-python-standard-library标准库。
系统详细信息:Windows 10 64 位、Python 2.6、Abaqus/CAE 6.12 或 6.14
测试脚本示例:
# multi.py
import multiprocessing
import time
def fib(n):
a,b = 0,1
for i in range(n):
a, b = a+b, a
return a
def workerfunc(num):
fname = ''.join(('worker_', str(num), '.txt'))
with open(fname, 'w') as f:
f.write('Starting Worker {0}\n'.format(num))
count = 0
while count < 1000: # <-- Repeat a bunch of times.
count += 1
a=fib(20)
line = ''.join((str(a), '\n'))
f.write(line)
f.write('End Worker {0}\n'.format(num))
if __name__ == '__main__':
jobs = []
for i in range(2): # <-- Setting the number of processes manually
p = multiprocessing.Process(target=workerfunc, args=(i,))
jobs.append(p)
print 'starting', p
p.start()
print 'done starting', p
for j in jobs:
print 'joining', j
j.join()
print 'done joining', j
1A widely known finite element analysis package
2The script is a blend of a fairly standard Python function for fib()
, and examples from PyMOTW https://pymotw.com/2/multiprocessing/index.html