我测试python多处理并编写简单程序:
from multiprocessing import Process
from time import sleep
def f(name):
print 'hello', name
x=1
while True:
x+=1
sleep(1)
print 'subprocess '+str(x)
if x==10:
quit()
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
p.start()
x=1
while True:
x+=1
sleep(0.1)
print 'main process '+str(x)
if x==100:
quit()
它的工作,但我没有错误:
Traceback (most recent call last):
File "ttt.py", line 17, in <module>
p.start()
File "/usr/lib64/python2.6/multiprocessing/process.py", line 104, in start
self._popen = Popen(self)
File "/usr/lib64/python2.6/multiprocessing/forking.py", line 99, in __init__
code = process_obj._bootstrap()
File "/usr/lib64/python2.6/multiprocessing/process.py", line 242, in _bootstrap
sys.stderr.write(e.args[0] + '\n')
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
答案 0 :(得分:1)
使用sys.exit()
代替quit()
。后者仅用于交互式解释器。
正如Kevin所说,您可以使用return
中的f
来正常退出该功能。这可能更合适。