线程差异(target = sample_function),args = [arg1])thread.thread.thread(target = sample_function(arg1))
我一直在研究循环内的线程功能,这引起了我的注意。 如果我执行
threading.Thread(target=sample_function(arg1))
它,它实际上并不像预期的那样多线程,而是必须在启动第二个之前执行整个 sample_function()
。 事实证明我必须更改为这一行才能获得正确的多线程
threading.Thread(target=sample_function, args=[arg1])
有人能告诉我为什么会这样吗?
谢谢!
I've been working around a threading functionality inside a loop and this caught my attention.
If I execute
threading.Thread(target=sample_function(arg1))
It doesn't actually multithread as expected, instead it had to go through the entire sample_function()
before starting the second one.
Turns out i had to change to this line instead to get proper multithreading
threading.Thread(target=sample_function, args=[arg1])
Can anyone tell me why is this so?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
target=
预计是可调用的。sample_function
是可调用的。sample_function(arg1)
不可调用,而是函数调用的返回值。我假设
sample_function(arg1)
返回None
这使得threading.Thread(target=sample_function(arg1))
不运行任何内容。出于寓教于乐的目的:使示例函数返回一个字符串,看看会发生什么
The
target=
is expected to be a callable.sample_function
is callable.sample_function(arg1)
isn't callable, but the return of the function call.I assume that
sample_function(arg1)
returnsNone
which makesthreading.Thread(target=sample_function(arg1))
run nothing.For edutainment purposes: make samplefunction return a string and see what happens