Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python - Naming the Threads



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.

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

When you create a thread usingthreading.Thread() class, you can specify its name using thename 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

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

from threading import Threadimport threadingfrom time import sleepdef my_function_1(arg):   print("This tread name is", threading.current_thread().name)# Create thread objectsthread1 = 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 secondsthread1.start()thread1.join()# Start the second thread and wait for it to completethread2.start()thread2.join()

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

This tread name is MainThreadThis tread name is My_threadThis tread name is Thread-1 (my_function_1)

Dynamically Assigning Names to the Python Threads

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

Example

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

from threading import Threadimport threadingfrom time import sleepdef my_function_1(arg):   threading.current_thread().name = "custom_name"   print("This tread name is", threading.current_thread().name)# Create thread objectsthread1 = 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 secondsthread1.start()thread1.join()# Start the second thread and wait for it to completethread2.start()thread2.join()

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

This tread name is MainThreadThis tread name is custom_nameThis 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 threadingdef addition_of_numbers(x, y):   print("This Thread name is :", threading.current_thread().name)   result = x + ydef 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 namest1 = 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 threadst1.start()t1.join()t2.start()t2.join()t3.name = 'custom_name'  # Assigning name after thread creationt3.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_threadThis Thread name is : Thread-1 (cube_number)This Thread name is : custom_nameMainThread
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp