简体   繁体   English

python 多进程:父进程无法退出

[英]python multiprocess: parent process can not exit

I want parent process exit, and child process can still run.我希望父进程退出,子进程仍然可以运行。
When i run code, parent process can not exit.当我运行代码时,父进程无法退出。 I use return to exit parent process.我使用return退出父进程。 And child process can still run.子进程仍然可以运行。
Why did parent process not exit?为什么父进程没有退出?

System: centos7系统:centos7
Python version: 3.6 Python 版本:3.6

import os
import time
from multiprocessing import Process


def run_process():
    count = 0
    while True:
        print('it is son', os.getpid())
        time.sleep(3)
        count += 1
        if count >= 10:
            return


def main():
    t = Process(target=run_process)
    t.start()
    print('it is father', os.getpid())
    return

if __name__ == '__main__':
    main()

I suppose this happens because multiprocessing registering atexit handler.我想这是因为multiprocessing注册atexit处理程序。

source1 - registering in util module source1 - 在util模块中注册

source2 - util module import source2 - util模块导入

Probably you'd better use simple os.fork for your purposes.可能你最好使用简单的os.fork来达到你的目的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM