Python 简明教程

Python - Naming the Threads

在 Python 中,给线程命名涉及将一个字符串指定为线程对象的身份标识符。Python 中的线程名称主要仅用于识别目的,不影响线程的行为或语义。多个线程可以共享同一个名称,而名称可以在线程初始化期间指定或动态更改。

In Python, naming a thread involves assigning a string as an identifier to the thread object. Thread names in Python are primarily used for identification purposes only and do not affect the thread’s behavior or semantics. Multiple threads can share the same name, and names can be specified during the thread’s initialization or changed dynamically.

Python 中的线程命名提供了一种直接的方式在并发程序中识别和管理线程。通过指定有意义的名称,用户可以增强代码清晰度,轻松调试复杂的多线程应用程序。

Thread naming in Python provides a straightforward way to identify and manage threads within a concurrent program. By assigning meaningful names, users can enhance code clarity and easily debug the complex multi-threaded applications.

Naming the Threads in Python

当使用 threading.Thread() 类创建线程时,可以用 name 参数指定它的名称。如果未提供,Python 将分配一个默认名称,类似于以下模式“Thread-N”,其中 N 是一个小数。或者,如果指定了目标函数,则默认名称格式变为“Thread-N (target_function_name)”。

When you create a thread using threading.Thread() class, you can specify its name using the name parameter. If not provided, Python assigns a default name like the following pattern "Thread-N", where N is a small decimal number. Alternatively, if you specify a target function, the default name format becomes "Thread-N (target_function_name)".

Example

这是一个用 threading.Thread() 类创建的线程分配自定义名称和默认名称的示例,它显示了名称如何反映目标函数。

Here is an example demonstrates assigning custom and default names to threads created using threading.Thread() class, and displays how names can reflect target functions.

from threading import Thread
import threading
from time import sleep

def my_function_1(arg):
   print("This tread name is", threading.current_thread().name)

# Create thread objects
thread1 = Thread(target=my_function_1, name='My_thread', args=(2,))
thread2 = Thread(target=my_function_1, args=(3,))

print("This tread name is", threading.current_thread().name)

# Start the first thread and wait for 0.2 seconds
thread1.start()
thread1.join()

# Start the second thread and wait for it to complete
thread2.start()
thread2.join()

在执行上述操作后,它将生成以下结果 −

On executing the above, it will produce the following results −

This tread name is MainThread
This tread name is My_thread
This tread name is Thread-1 (my_function_1)

Dynamically Assigning Names to the Python Threads

你可以通过直接修动线程对象的 name 属性动态地分配或更改一个线程的名称。

You can assign or change a thread’s name dynamically by directly modifying the name attribute of the thread object.

Example

此示例显示了如何通过修改线程对象的 name 属性动态地更改线程名称。

This example shows how to dynamically change thread names by modifying the name attribute of the thread object.

from threading import Thread
import threading
from time import sleep

def my_function_1(arg):
   threading.current_thread().name = "custom_name"
   print("This tread name is", threading.current_thread().name)

# Create thread objects
thread1 = Thread(target=my_function_1, name='My_thread', args=(2,))
thread2 = Thread(target=my_function_1, args=(3,))

print("This tread name is", threading.current_thread().name)

# Start the first thread and wait for 0.2 seconds
thread1.start()
thread1.join()

# Start the second thread and wait for it to complete
thread2.start()
thread2.join()

在执行上面的代码后,它将产生以下结果 −

When you execute the above code, it will produce the following results −

This tread name is MainThread
This tread name is custom_name
This tread name is custom_name

Example

线程可以在创建后使用自定义名称初始化,甚至重命名。此示例演示了使用自定义名称创建线程和修改创建后线程的名称。

Threads can be initialized with custom names and even renamed after creation. This example demonstrates creating threads with custom names and modifying a thread’s name after creation.

import threading

def addition_of_numbers(x, y):
   print("This Thread name is :", threading.current_thread().name)
   result = x + y

def cube_number(i):
   result = i ** 3
   print("This Thread name is :", threading.current_thread().name)

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

# Create threads with custom names
t1 = threading.Thread(target=addition_of_numbers, name='My_thread', args=(2, 4))
t2 = threading.Thread(target=cube_number, args=(4,))
t3 = threading.Thread(target=basic_function)

# Start and join threads
t1.start()
t1.join()

t2.start()
t2.join()

t3.name = 'custom_name'  # Assigning name after thread creation
t3.start()
t3.join()

print(threading.current_thread().name)  # Print main thread's name

执行后,上面的代码将生成以下结果 −

Upon execution, the above code will produce the following results −

This Thread name is : My_thread
This Thread name is : Thread-1 (cube_number)
This Thread name is : custom_name
MainThread