Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中线程中的多处理不';不要让每个子进程都工作_Python_Multithreading_Multiprocessing - Fatal编程技术网

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(“过程日志”)

在python中使用
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位

我假设您使用的是类UNIX系统?因为如果是这样,你在这里做坏事,Python在类UNIX系统上默认实现的
Process
使用
fork
,因此通过在线程中启动
Process
es,就可以从多线程进程中执行
fork

这里有两种解决方案:

  • 停止使用线程。这里不需要线程,因为它们实际上并不管理生命周期(线程和进程都是
    守护进程
    s,因此当主进程的主线程完成时,它们将被不规则地杀死)
  • 如果您必须使用线程,并且您使用的是Python 3.4+,那么您可以切换(或者使用
    '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