简体   繁体   English

Python:Child进程退出,使其线程继续运行

[英]Python:Child process exits leaving its thread running

While working on a project using the multiprocessing module in python I observed some strange behaviour. 在使用python中的multiprocessing模块multiprocessing项目时,我观察到一些奇怪的行为。

Lets assume that my main program p1 created a process p2 using multiprocessing module. 假设我的主程序p1使用multiprocessing模块创建了一个进程p2 The process p2 created a thread in itself called p2_t1 . 该过程P2创造本身就是一个线程调用p2_t1。 There is no code block in p2 after creating this thread, so it exits(i am NOT calling exit, so it would just return from main) leaving p2_t1 dangling. 创建此线程后, p2中没有代码块,因此它退出(我不调用exit,所以它只会从main返回)而使p2_t1悬空。 I can confirm this via strace 我可以通过strace确认

Example code in p1 that creates a child process p1中创建子进程的示例代码

p = Process(target=RunService,args=(/*some args*/),name="p2")

Example code of p2 : p2的示例代码:

def RunService():
    try:
        /*do something here*/
    except Exception,e:
        /*create a new thread*/

    /*nothing here so basically this process exits leaving the thread dangling*/

This however does not happen if a thread(call is p1_t1 ) is created within p1 . 但是,如果在p1中创建了线程(调用为p1_t1 ),则不会发生这种情况。 p1 doesnot exit until the created thread p1_t1 runs. 在创建的线程p1_t1运行之前, p1不会退出。

Example code in this case for p1 在这种情况下, p1的示例代码

try:
  /*do something here*/
except Exception,e:
  /*create a new thread*/

    /*nothing here so basically process should end*/

In this case the process doesnot exit and keeps running until the thread is running. 在这种情况下,该进程不会退出并保持运行,直到线程运行为止。 Any explanations? 有什么解释吗?

Without any code to speak of, it is hard to tell what's happening, but I would try making your thread a daemon thread and then wait on it with p2. 没有任何代码可言,很难说出正在发生什么,但是我会尝试将您的线程变成守护线程,然后使用p2等待它。 When the tread finished, then p2 should shut down properly, killing off the thread. 胎面完成后,p2应该正确关闭,从而终止螺纹。 Something like this: 像这样:

t1 = threading.Thread(target=somefunc, args=((2,)))
t1.setDaemon(True)
t1.start()
t1.join()

good luck, Mike 麦克,祝你好运

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

相关问题 在父进程退出后让子进程继续运行 - Leaving child process running after the parent exits 在 Node.js 中运行 Python 脚本的带有 child_process 的 Promise - 进程在所有数据之前退出 - A Promise with child_process running a Python script in Node.js - Process exits before all data 运行Python多线程进程并通过信号中断子线程 - Running Python multi-threaded process & interrupt a child thread with a signal python守护程序线程退出,但进程仍在后台运行 - python daemon thread exits but process still run in the background 如果任何并行线程退出,如何退出 Python 进程? - How to exit a Python process if any parallel thread exits? 在自己的线程或进程中运行`mainloop()`? - Running `mainloop()` in its own thread or process? git 进程作为 Python 子进程运行时从不退出 - The git process never exits when running as a Python subprocess 完成线程的进程永远不会退出 - Process with finished thread never exits Python:为长时间运行的后台进程生成或线程? - Python: spawn or thread for long running background process? 为什么子进程运行其父代的代码并如何防止它? - Why is child process running code of its parent and how to prevent it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM