使用多处理和线程的竞争条件

时间:2012-08-28 12:34:20

标签: python multithreading multiprocessing

我写了示例程序。 它创建了8个线程,并在每个线程中生成进程

import threading
from multiprocessing import Process

def fast_function():
    pass

def thread_function():
    process_number = 1
    print 'start %s processes' % process_number
    for i in range(process_number):
        p = Process(target=fast_function, args=())
        p.start()
        p.join()

def main():
    threads_number = 8
    print 'start %s threads' % threads_number
    threads = [threading.Thread(target=thread_function, args=()) 
            for i in range(threads_number)]

    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()

它崩溃了几个例外,比如

Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.6/threading.py", line 484, in run
    self.__target(*self.__args, **self.__kwargs)
  File "./repeat_multiprocessing_bug.py", line 15, in thread_function
    p.start()
  File "/usr/lib/python2.6/multiprocessing/process.py", line 99, in start
    _cleanup()
  File "/usr/lib/python2.6/multiprocessing/process.py", line 53, in _cleanup
    if p._popen.poll() is not None:
  File "/usr/lib/python2.6/multiprocessing/forking.py", line 106, in poll
    pid, sts = os.waitpid(self.pid, flag)
OSError: [Errno 10] No child processes

Python版本2.6.5。有人可以解释我做错了吗?

2 个答案:

答案 0 :(得分:1)

您可能正试图从交互式解释器运行它。尝试将代码写入文件并将其作为python脚本运行,它可以在我的机器上运行...

请参阅Python multiprocessing docs上的说明和示例。

答案 1 :(得分:1)

多处理模块在2.6.5中存在线程安全问题。您最好的选择是更新到更新的Python,或者将此补丁添加到2.6.5:http://hg.python.org/cpython/rev/41aef062d529/

该错误在以下链接中有更详细的描述: