为什么 current_thread() 总是 return MainThread?
Why current_thread() always return MainThread?
我在 python 开始学习线程。
我在这里看到这段代码说如果我想同时运行 2个线程
我正在学习免费的 udemy 课程,这就是他打印线程名称的方式。
只有他得到"thread-1"、"thread-2"。
我应该这样做:
if __name__ == '__main__':
Thread(target = displayNums()).start()
Thread(target = stam()).start()
在这里看到的:
Make 2 functions run at the same time
现在,我要做的是同时 运行 2 或 3 个线程,并获取它们的名称。
但我总是得到 MainThread
我的代码:
from threading import *
def displayNums():
i = 0
print(current_thread().getName() , "suo")
while i <= 10:
print(i)
i+=1
print(current_thread().getName())
t = Thread(target=displayNums())
def stam():
k = 0
print(current_thread().getName(), "sss")
while k <= 50:
print(k,"k")
k+=1
if __name__ == '__main__':
Thread(target = displayNums()).start()
Thread(target = stam()).start()
我没有收到任何错误,但我得到以下输出:
MainThread
MainThread suo
0
1
2
3
4
5
6
7
8
9
10
MainThread suo
0
1
2
3
4
5
6
7
8
9
10
MainThread sss
0 k
1 k
2 k
3 k
4 k
5 k
6 k
7 k
8 k
9 k
10 k
11 k
12 k
13 k
14 k
15 k
16 k
17 k
18 k
19 k
20 k
21 k
22 k
23 k
and so on...
您的代码实际上是在线程外调用线程目标。将您的代码编辑为:
if __name__ == '__main__':
Thread(target = displayNums).start()
Thread(target = stam).start()
改变这个:
if __name__ == '__main__':
Thread(target = displayNums()).start()
Thread(target = stam()).start()
对此:
if __name__ == '__main__':
Thread(target = displayNums).start()
Thread(target = stam).start()
线程目标是函数本身,而不是函数的返回值。 stam()
调用函数并且 returns 它的值,但是 stam
是函数对象本身。当您创建一个线程时,您需要函数本身,而不是它的返回值。
编辑:当您想 运行 立即调用一个函数时。您可以访问它的返回值,例如 x = f()
会将 f 的返回值赋给 x。在其他情况下,您现在不想 运行 一个函数,但又想跟踪它以便以后调用它。线程就是这种情况。你不想 运行 函数,你想告诉线程构造函数在第二个线程中 运行 什么函数。所以在那种情况下,你将函数本身作为参数传递。通常这意味着没有任何括号的函数名称。
Python 像对待任何其他对象一样对待函数。在 Python 中,您可以将函数分配给变量,然后变量就所有意图和目的而言就是函数本身。它可以像原始函数一样调用(带大括号),也可以传递给函数,或其他任何方式。
试试这个:
# print is a function (built-in), and I can assign it to a variable
a = print
# Nothing gets printed yet
# the variable 'a' is now the same thing as the print function
a("Hello world")
这是一个愚蠢的例子,但在许多现实世界的情况下这非常有用。
我在 python 开始学习线程。
我在这里看到这段代码说如果我想同时运行 2个线程
我正在学习免费的 udemy 课程,这就是他打印线程名称的方式。
只有他得到"thread-1"、"thread-2"。
我应该这样做:
if __name__ == '__main__':
Thread(target = displayNums()).start()
Thread(target = stam()).start()
在这里看到的: Make 2 functions run at the same time
现在,我要做的是同时 运行 2 或 3 个线程,并获取它们的名称。 但我总是得到 MainThread 我的代码:
from threading import *
def displayNums():
i = 0
print(current_thread().getName() , "suo")
while i <= 10:
print(i)
i+=1
print(current_thread().getName())
t = Thread(target=displayNums())
def stam():
k = 0
print(current_thread().getName(), "sss")
while k <= 50:
print(k,"k")
k+=1
if __name__ == '__main__':
Thread(target = displayNums()).start()
Thread(target = stam()).start()
我没有收到任何错误,但我得到以下输出:
MainThread
MainThread suo
0
1
2
3
4
5
6
7
8
9
10
MainThread suo
0
1
2
3
4
5
6
7
8
9
10
MainThread sss
0 k
1 k
2 k
3 k
4 k
5 k
6 k
7 k
8 k
9 k
10 k
11 k
12 k
13 k
14 k
15 k
16 k
17 k
18 k
19 k
20 k
21 k
22 k
23 k
and so on...
您的代码实际上是在线程外调用线程目标。将您的代码编辑为:
if __name__ == '__main__':
Thread(target = displayNums).start()
Thread(target = stam).start()
改变这个:
if __name__ == '__main__':
Thread(target = displayNums()).start()
Thread(target = stam()).start()
对此:
if __name__ == '__main__':
Thread(target = displayNums).start()
Thread(target = stam).start()
线程目标是函数本身,而不是函数的返回值。 stam()
调用函数并且 returns 它的值,但是 stam
是函数对象本身。当您创建一个线程时,您需要函数本身,而不是它的返回值。
编辑:当您想 运行 立即调用一个函数时。您可以访问它的返回值,例如 x = f()
会将 f 的返回值赋给 x。在其他情况下,您现在不想 运行 一个函数,但又想跟踪它以便以后调用它。线程就是这种情况。你不想 运行 函数,你想告诉线程构造函数在第二个线程中 运行 什么函数。所以在那种情况下,你将函数本身作为参数传递。通常这意味着没有任何括号的函数名称。
Python 像对待任何其他对象一样对待函数。在 Python 中,您可以将函数分配给变量,然后变量就所有意图和目的而言就是函数本身。它可以像原始函数一样调用(带大括号),也可以传递给函数,或其他任何方式。
试试这个:
# print is a function (built-in), and I can assign it to a variable
a = print
# Nothing gets printed yet
# the variable 'a' is now the same thing as the print function
a("Hello world")
这是一个愚蠢的例子,但在许多现实世界的情况下这非常有用。