gpt4 book ai didi

python - 同时使用多处理和线程的竞争条件

转载 作者:行者123 更新时间:2023-11-28 17:49:51 26 4
gpt4 key购买 nike

我写了示例程序。它创建 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。有人可以解释我做错了什么吗?

最佳答案

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

请参阅 Python multiprocessing docs 中的说明和示例.

关于python - 同时使用多处理和线程的竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12159328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com