简体   繁体   English

启动第二个进程时出现Python多处理错误

[英]Python multiprocessing error when start 2nd process

Today i'm started in multiprocessing, and of course has some problems. 今天,我开始进行多处理,当然有一些问题。

I create two processes: 我创建两个过程:

import multiprocessing as mp

# twitter parser
twitterProc = mp.Process ( target=TwitterProcess )
twitterProc.start()

# rss parser
RssProc = mp.Process ( target=RssProcess )
RssProc.start()

When code start RssProc it work other 1-2 seconds, start fist process (twitterProc) and it work normally, then starts second process and after 1-2 seconds it stopped with such error list: 当代码启动RssProc时,它会再工作1-2秒,启动第一个进程(twitterProc)并正常工作,然后启动第二个进程,并在1-2秒后停止,并显示以下错误列表:

Connected to pydev debugger (build 172.3757.67)
Traceback (most recent call last):
Error in atexit._run_exitfuncs:  File "_pydevd_bundle/pydevd_cython.pyx", line 735, in _pydevd_bundle.pydevd_cython.PyDBFrame.trace_dispatch (_pydevd_bundle/pydevd_cython.c:15637)

Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/util.py", line 325, in _exit_function
p.join()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 145, in join
self.do_wait_suspend(thread, frame, event, arg)
File "_pydevd_bundle/pydevd_cython.pyx", line 252, in _pydevd_bundle.pydevd_cython.PyDBFrame.do_wait_suspend (_pydevd_bundle/pydevd_cython.c:5643)
self._args[0].do_wait_suspend(*args, **kwargs)
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 767, in do_wait_suspend
res = self._popen.wait(timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/forking.py", line 154, in wait
return self.poll(0)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/forking.py", line 135, in poll pid, sts = os.waitpid(self.pid, flag)
KeyboardInterrupt
time.sleep(0.01)
KeyboardInterrupt
Process Process-RSS:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
Error in sys.exitfunc:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/util.py", line 325, in _exit_function
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 114, in run
p.join()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 145, in join
self._target(*self._args, **self._kwargs)
File "/Users/Stas/PycharmProjects/untitled/streambot.py", line 674, in __init__
res = self._popen.wait(timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/forking.py", line 154, in wait
return self.poll(0)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/forking.py", line 135, in poll
pid, sts = os.waitpid(self.pid, flag)
KeyboardInterrupt
for source in config.feedList:
File "/Users/Stas/PycharmProjects/untitled/streambot.py", line 674, in __init__
for source in config.feedList:
File "_pydevd_bundle/pydevd_cython.pyx", line 970, in _pydevd_bundle.pydevd_cython.SafeCallWrapper.__call__ (_pydevd_bundle/pydevd_cython.c:19428)
ret = (<object>method_obj)(*args)
File "_pydevd_bundle/pydevd_cython.pyx", line 501, in _pydevd_bundle.pydevd_cython.PyDBFrame.trace_dispatch (_pydevd_bundle/pydevd_cython.c:18762)
cpdef trace_dispatch(self, frame, str event, arg):
File "_pydevd_bundle/pydevd_cython.pyx", line 744, in _pydevd_bundle.pydevd_cython.PyDBFrame.trace_dispatch (_pydevd_bundle/pydevd_cython.c:15791)
raise
File "_pydevd_bundle/pydevd_cython.pyx", line 735, in _pydevd_bundle.pydevd_cython.PyDBFrame.trace_dispatch (_pydevd_bundle/pydevd_cython.c:15637)
self.do_wait_suspend(thread, frame, event, arg)
File "_pydevd_bundle/pydevd_cython.pyx", line 252, in _pydevd_bundle.pydevd_cython.PyDBFrame.do_wait_suspend (_pydevd_bundle/pydevd_cython.c:5643)
self._args[0].do_wait_suspend(*args, **kwargs)
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 767, in do_wait_suspend
time.sleep(0.01)
KeyboardInterrupt

Process finished with exit code 0

What is that and how to solve it? 那是什么以及如何解决?

I think best way is to use join method after creation of the Process. 我认为最好的方法是在创建Process后使用join方法。 join informs the main thread to wait until the process completes it's work join通知主线程等待,直到该进程完成工作为止

twitterProc.join() and RssProc.join() at the end of your script 脚本末尾的twitterProc.join()RssProc.join()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM