You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中Process.join()在Windows与Linux下的行为差异原因探究

Windows 10与Ubuntu 18.04中Python 3.6 Process.join()的行为差异原因问询

我发现Windows 10和Ubuntu 18.04系统在Python 3.6中对Process.join()函数的处理存在行为差异,特此询问原因:

以下代码中,主进程启动子进程后调用join(),子进程内部启动了一个非守护线程但未终止它。在Windows系统中,join()会持续阻塞,主进程一直运行;而在Linux系统中,子进程会结束,主进程也随之结束。打印线程信息的语句显示,该线程在Windows和Linux下均为非守护线程,且代码未指定子进程采用fork或spawn方式创建。

测试代码

import threading
import time
from multiprocessing import Process

def thread_f():
    print("Thread function")
    while True:
        time.sleep(1)

def subprocess_f():
    print("TOP of Subprocess function")
    thread = threading.Thread(target=thread_f)
    thread.start()
    threads = ', '.join(map(str, [(thread.name, thread.ident, "isDaemon {}".format(thread.isDaemon())) for thread in
                                  threading.enumerate()]))
    print(threads)
    print("BOTTOM of Subprocess function")

def main():
    print("TOP of main")
    process = Process(target=subprocess_f)
    process.start()
    process.join()
    print("BOTTOM of main")

# In Windows 10, main process continues to run.
# In Ubuntu Linux 18.04, main process ends.

if __name__ == '__main__':
    main()

Windows系统输出

TOP of main
TOP of Subprocess function
Thread function
('MainThread', 14952, 'isDaemon False'), ('Thread-1', 10992, 'isDaemon False')
BOTTOM of Subprocess function

Ubuntu 18.04系统输出

TOP of main
TOP of Subprocess function
Thread function
('MainThread', 140283868624704, 'isDaemon False'), ('Thread-1', 140283844200192, 'isDaemon False')
BOTTOM of Subprocess function
BOTTOM of main

内容的提问来源于stack exchange,提问作者Jacob Quisenberry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 04:57:09