1

我测试 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'
4

1 回答 1

1

使用sys.exit()而不是quit(). 后者仅用于交互式解释器。

正如 Kevin 所说,您可以使用returninf正常退出该功能。这也许更合适。

于 2012-08-16T11:56:23.510 回答