当没有 .join() 时,python 中的线程会发生什么? [英] What happens to threads in python when there is no .join()?

查看:64
本文介绍了当没有 .join() 时,python 中的线程会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个多线程 Python 代码,如下所示:

Suppose, we have a multi-thread Python code which looks like this:

import threading
import time

def short_task():
    print 'Hey!'

for x in range(10000):
    t = threading.Thread(target=short_task)
    t.daemon = True  # All non-daemon threads will be ".join()"'ed when main thread dies, so we mark this one as daemon
    t.start()

time.sleep(100)

在长时间运行的应用程序(例如 Django+uwsgi)中使用类似方法是否有任何副作用?比如没有垃圾回收、额外的内存消耗等?

Are there any side-effects from using similar approach in long-running applications (e.g. Django+uwsgi)? Like no garbage collection, extra memory consumption, etc?

我想做的是在不阻塞主线程的情况下执行一些代价高昂的日志记录(urlopen() 到外部 API url).在没有 .join() 的情况下产生无限的新线程在这里看起来是最好的方法,但也许我错了?

What I am trying to do is to do some costly logging (urlopen() to external API url) without blocking the main thread. Spawning infinite new threads with no .join() looks like best possible approach here, but maybe I am wrong?

推荐答案

不是 100% 自信的答案,但由于没有其他人考虑过...

Not a 100% confident answer, but since nobody else has weighed in...

我在 Python 文档中找不到任何地方说您必须加入线程.Python 的线程模型在我看来类似于 Java:在 Java 中 t.join() 的意思是等待 t 死亡",但它并不意味着其他任何东西.特别是,t.join() 对线程 t 没有任何作用.

I can't find any place in the Python documentation that says you must join threads. Python's threading model looks Java-like to me: In Java t.join() means "wait for t to die," but it does not mean anything else. In particular, t.join() does not do anything to thread t.

我不是专家,但在 Python 中看起来也是如此.

I'm not an expert, but it looks like the same is true in Python.

是否有任何副作用...比如...额外的内存消耗

Are there any side-effects...Like...extra memory consumption

每个 Python 线程都必须有自己的、固定大小的调用堆栈,threading 模块文档说堆栈的最小大小为 32K 字节.如果您创建一万个这样的代码片段,并且它们都设法同时存在,那么仅堆栈就将占用 320 兆字节的实际内存.

Every Python thread must have its own, fixed-size call stack, and the threading module documentation says that the minimum size of a stack is 32K bytes. If you create ten thousand of those, like in your code snippet, and if they all manage to exist at the same time, then just the stacks alone are going to occupy 320 megabytes of real memory.

为一个程序有这么多并发线程找到一个很好的理由是不寻常的.

It's unusual to find a good reason for a program to have that many simultaneous threads.

如果您希望这些线程死得如此之快,以至于同时存在的线程绝不会超过几个,那么您可能可以通过使用 线程池.线程池是一个管理少量工作线程和一个任务阻塞队列的对象(即功能对象).每个工作人员都坐在一个循环中,从队列中挑选任务并执行它们.

If you're expecting those threads to die so quickly that there's never more than a few of them living at the same time, then you probably could improve the performance of your program by using a thread pool. A thread pool is an object that manages a small number of worker threads and a blocking queue of tasks (i.e., functional objects). Each worker sits in a loop, picking tasks from the queue and performing them.

有效地使用线程池的程序重用其工作线程,而不是不断地让线程死掉并创建新的线程来替换它们.

A program that uses a thread pool effectively re-uses its worker threads instead of continually letting threads die and creating new ones to replace them.

这篇关于当没有 .join() 时,python 中的线程会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆