Python 使用 threading.Thread.join()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19138219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of threading.Thread.join()
提问by Pratik Singhal
I am new to multithreading in python and trying to learn multithreading using threading module. I have made a very simple program of multi threading and i am having trouble understanding the threading.Thread.join
method.
我是 python 中多线程的新手,并尝试使用线程模块学习多线程。我已经制作了一个非常简单的多线程程序,但我无法理解该threading.Thread.join
方法。
Here is the source code of the program I have made
这是我制作的程序的源代码
import threading
val = 0
def increment():
global val
print "Inside increment"
for x in range(100):
val += 1
print "val is now {} ".format(val)
thread1 = threading.Thread(target=increment, args=())
thread2 = threading.Thread(target=increment, args=())
thread1.start()
#thread1.join()
thread2.start()
#thread2.join()
What difference does it make if I use
如果我使用它有什么区别
thread1.join()
thread2.join()
which I have commented in the above code? I ran both the source codes (one with comments and the one without comments) but the output is the same.
我在上面的代码中评论了什么?我运行了两个源代码(一个有注释,一个没有注释),但输出是一样的。
采纳答案by Erik Kaplun
A call to thread1.join()
blocks the thread in which you're making the call, until thread1
is finished. It's like wait_until_finished(thread1)
.
调用thread1.join()
阻塞您正在调用的线程,直到thread1
完成。就像wait_until_finished(thread1)
.
For example:
例如:
import time
def printer():
for _ in range(3):
time.sleep(1.0)
print "hello"
thread = Thread(target=printer)
thread.start()
thread.join()
print "goodbye"
prints
印刷
hello
hello
hello
goodbye
—without the .join()
call, goodbye
would come first and then 3 * hello
.
- 没有.join()
电话,goodbye
会先来,然后是 3 * hello
。
Also, note that threads in Python do not provide any additional performance (in terms of CPU processing power) because of a thing called the Global Interpreter Lock, so while they are useful for spawning off potentially blocking (e.g. IO, network) and time consuming tasks (e.g. number crunching) to keep the main thread free for other tasks, they do not allow you to leverage multiple cores or CPUs; for that, look at multiprocessing
which uses subprocesses but exposes an API equivalent to that of threading
.
另外,请注意 Python 中的线程不提供任何额外的性能(就 CPU 处理能力而言),因为有一种叫做Global Interpreter Lock的东西,所以虽然它们对于产生潜在的阻塞(例如 IO、网络)和耗时任务(例如数字运算)以保持主线程空闲用于其他任务,它们不允许您利用多个内核或 CPU;为此,请查看multiprocessing
哪些使用子流程但公开了与threading
.
PLUG:...and it is also for the above reason that, if you're interested in concurrency, you might also want to look into a fine library called Gevent, which essentially just makes threading much easier to use, much faster (when you have many concurrent activities) and less prone to concurrency related bugs, while allowing you to keep coding the same way as with "real" threads. Also Twisted, Eventlet, Tornado and many others, are either equivalent or comparable. Furthermore, in any case, I'd strongly suggest reading these classics:
PLUG:...而且也是出于上述原因,如果您对并发感兴趣,您可能还想查看一个名为 Gevent 的优秀库,它本质上只是使线程更易于使用,速度更快(当您有许多并发活动)并且不太容易出现与并发相关的错误,同时允许您以与“真实”线程相同的方式进行编码。此外,Twisted、Eventlet、Tornado 和许多其他产品是等效的或可比较的。此外,无论如何,我强烈建议阅读这些经典著作:
回答by goncalopp
As the relevant documentationstates, join
makes the caller wait until the thread terminates.
正如相关文档所述,join
使调用者等待线程终止。
In your case, the output is the same because join
doesn't change the program behaviour - it's probably being used to exit the program cleanly, only when all the threads have terminated.
在您的情况下,输出是相同的,因为join
不会改变程序行为 - 它可能用于干净地退出程序,仅当所有线程都终止时。
回答by aaj
I modified the code so that you will understand how exactly join works. so run this code with comments and without comments and observe the output for both.
我修改了代码,以便您了解 join 的工作原理。因此,运行带有注释和不带注释的代码并观察两者的输出。
val = 0
def increment(msg,sleep_time):
global val
print "Inside increment"
for x in range(10):
val += 1
print "%s : %d\n" % (msg,val)
time.sleep(sleep_time)
thread1 = threading.Thread(target=increment, args=("thread_01",0.5))
thread2 = threading.Thread(target=increment, args=("thread_02",1))
thread1.start()
#thread1.join()
thread2.start()
#thread2.join()