简体   繁体   English

如何不等待线程在Python中完成

[英]How NOT to wait for a thread to finish in Python

In this question , he actually asked something like what I want. 在这个问题上 ,他实际上问的是我想要的东西。 Except that the answer was to remove the parentheses. 答案是删除括号。 However if I remove the parentheses, then I'll not be able to pass arguments for my functions. 但是,如果删除括号,则无法传递函数的参数。

How can I do the following simple code without waiting: 如何不等待就执行以下简单代码:

from time import sleep
import threading

def whatever(i):
  sleep(5)
  print("Hey! It's me number " + str(i))

for i in range(3):
  t = threading.Thread(target=whatever(i))
  t.start()

Desired output would be 所需的输出将是

Hey! It's me number 0
Hey! It's me number 1
Hey! It's me number 2

All printed at the same time 全部同时打印

From the documentation , target should be a callable: 从文档开始target应该是可调用的:

target is the callable object to be invoked by the run() method target是run()方法要调用的可调用对象

You are not passing your function to target , you are passing the return value of your function, so the function runs as soon as you pass it to threading.Thread , not when you call t.start() . 您没有将函数传递给target ,而是传递了函数的返回值,因此该函数在将其传递给threading.Thread立即运行,而不是在调用t.start()

You should be using the args parameter to specify arguments to your callable. 您应该使用args参数为可调用对象指定参数。

Simply change this line: 只需更改此行:

 t = threading.Thread(target=whatever(i))

to

t = threading.Thread(target=whatever, args=(i,))

you seem to not realize what this line does: t = threading.Thread(target=whatever(i)) , removing the parenthesis is not simply to not wait for the thread to finish its so you can actually start the function in a seperate thread: 您似乎没有意识到这一行的作用: t = threading.Thread(target=whatever(i)) ,删除括号并不是简单地不等待线程完成操作,因此您实际上可以在单独的线程中启动函数:

target should be the function object itself, but when you do t = threading.Thread(target=whatever(1)) , the target will be the return value of whatever that you already ran in your original thread, you need to give threading the function itself then specify the parameters seperately and it will call it for you like this: target应该是函数对象本身,但是当您执行t = threading.Thread(target=whatever(1))target将是您已经在原始线程中运行的whatever target的返回值,您需要给threading加函数本身然后分别指定参数,它将像这样为您调用:

from time import sleep
import threading

def whatever(i):
  sleep(5)
  print("Hey! It's me number " + str(i))

for i in range(3):
  t = threading.Thread(target=whatever, args=(i,))
  t.start()
from time import sleep
import threading

def whatever(i):
  print("Hey! It's me number " + str(i))

for i in range(3):
  t = threading.Thread(target=whatever, args=(i,))
  t.start()

You have to consider one thing though. 但是,您必须考虑一件事。

In Python we have something called GIL - Global Interpreter Lock. 在Python中,我们有一个叫做GIL的东西-全局解释器锁定。 It's something that, in short, makes it possible for only one thread of your python application, to execute in a given interpreter at once. 简而言之,这使得您的python应用程序中只有一个线程可以立即在给定的解释器中执行。 What does it mean? 这是什么意思?

That it's not quite that easy do achieve true concurrency in Python - while it may seem like the instructions are being executed simultaneously because of the super quick CPUs we have today, they are, in fact, not. 在Python中实现真正的并发并不那么容易-尽管由于我们今天拥有的超快速CPU,似乎指令正在同时执行,但实际上并非如此。

Just wrap it into a lambda. 只需将其包装成lambda。

from time import sleep
import threading

def whatever(i):
  sleep(5)
  print("Hey! It's me number " + str(i))

for i in range(3):
  t = threading.Thread(target=lambda: whatever(i))
  t.start()

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

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