python中线程中的多处理不';不要让每个子进程都工作
在python中使用python中线程中的多处理不';不要让每个子进程都工作,python,multithreading,multiprocessing,Python,Multithreading,Multiprocessing,在python中使用threading模块在每个线程中执行子进程时,某些进程不会像示例代码的输出那样正确启动和挂起 由于python中的IPC,启动进程似乎需要独占控制。是这样吗 使用螺纹锁,它可以完美地工作。我只是想制作一个线程来控制子进程的生死管理 #/usr/bin/env python #-*-编码:utf-8-*- #vim:fenc=utf-8 导入多处理 导入线程 进口火 从logzero导入记录器 def process_method(): logger.info(“过程日志”)
threading
模块在每个线程中执行子进程时,某些进程不会像示例代码的输出那样正确启动和挂起
由于python中的IPC,启动进程似乎需要独占控制。是这样吗
使用螺纹锁,它可以完美地工作。我只是想制作一个线程来控制子进程的生死管理
#/usr/bin/env python
#-*-编码:utf-8-*-
#vim:fenc=utf-8
导入多处理
导入线程
进口火
从logzero导入记录器
def process_method():
logger.info(“过程日志”)
def start_进程():
logger.info(“起始线程”)
过程=多处理。过程(目标=过程\方法)
process.daemon=True
process.start()
process.join()
def main(线程数=3):
线程=[]
对于范围内的(线程数):
t=线程。线程(目标=启动进程)
t、 daemon=True
t、 开始()
threads.append(t)
对于螺纹中的t:
t、 加入
logger.info(“程序完成”)
如果名称=“\uuuuu main\uuuuuuuu”:
火,火(主)
输出
环境
- python 3.6
- ubutu 64位
Process
使用fork
,因此通过在线程中启动Process
es,就可以从多线程进程中执行fork
这里有两种解决方案:
守护进程s,因此当主进程的主线程完成时,它们将被不规则地杀死)
'spawn'
方法使您的代码可移植到Windows,代价是在类UNIX系统上使进程的创建稍微慢一点),因此在启动任何线程之前,只需从主进程执行fork
,所有未来的fork
s都是从(无线程)fork服务器完成的。您所要做的就是添加多处理。将启动方法(“forkserver”)
设置为if uuuu name uuuu==“uuuu main”:
guard后的第一行旁注:我不知道如何实现
fire
或logzero
,因此它们完全可能不是线程安全的,也可能不是fork
安全的。如果您仍然有问题,您可能想在这里尝试简化为Python内置。非常感谢。是的,那是在Linux操作系统下。这是有道理的。
$ python '/tmp/tmp.lY3YDIltYg/test.py' 30
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:16] process log
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:20] starting thread
[I 190822 09:10:38 test:16] process log
[I 190822 09:10:38 test:16] process log
[I 190822 09:10:38 test:16] process log
[I 190822 09:10:38 test:16] process log
[I 190822 09:10:38 test:16] process log