Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么python线程模块中所有线程的名称都相同?_Python_Multithreading_Python 3.x_Python Multithreading - Fatal编程技术网

为什么python线程模块中所有线程的名称都相同?

为什么python线程模块中所有线程的名称都相同?,python,multithreading,python-3.x,python-multithreading,Python,Multithreading,Python 3.x,Python Multithreading,current_-thread().getName()也给出了相同的结果。我想知道它是这样工作的,还是运行同一个线程,所以它传递的是名称MainThread 输出: 这是主线程 这是主线程这是主线程这是主线程当前线程()始终返回调用当前线程()的线程。您反复检索执行循环的线程的名称,而不是线程启动的任何线程的名称 如果要获取循环中启动的线程的名称,可以让它们调用current\u thread(): current\u-thread()始终返回调用current\u-thread()的线程。您

current_-thread().getName()
也给出了相同的结果。我想知道它是这样工作的,还是运行同一个线程,所以它传递的是名称
MainThread


输出:
这是主线程
这是主线程
这是主线程
这是主线程
当前线程()
始终返回调用当前线程()的线程。您反复检索执行循环的线程的名称,而不是线程启动的任何线程的名称

如果要获取循环中启动的线程的名称,可以让它们调用
current\u thread()

current\u-thread()
始终返回调用
current\u-thread()
的线程。您反复检索执行循环的线程的名称,而不是线程启动的任何线程的名称

如果要获取循环中启动的线程的名称,可以让它们调用
current\u thread()


name=current\u thread()。name
您总是检查当前线程的名称,它是主线程。你需要检查
t.name
。我想每次它进入循环时,它都会创建线程和当前线程()。name应该返回它的名称。为什么它返回名称
MainThread
,你不是从线程调用它。您只从主线程调用它。好的,当前线程是运行循环的线程吗?
name=current\u thread()。name
您总是检查当前线程的名称,它是主线程。你需要检查
t.name
。我想每次它进入循环时,它都会创建线程和当前线程()。name应该返回它的名称。为什么它返回名称
MainThread
,你不是从线程调用它。您只从主线程调用它。好的,当前线程是运行循环的线程,对吗?
from threading import *

def myfunc(i,name):
    print("This is " + str(name))

for i in range(4):
    name = current_thread().name
    t = Thread(target=myfunc, args=(i,name,))
    t.start()
import threading

def target():
    print("This is", threading.current_thread().name)

for i in range(4):
    Thread(target=target).start()