I just name my threads when I create them.
Example thread:
import threading
from threading import Thread
#...
client_address = '192.168.0.2'
#...
thread1 = Thread(target=thread_for_receiving_data,
args=(connection, q, rdots),
name='Connection with '+client_address,
daemon=True)
thread1.start()
Then you can always access the name from inside the thread
print(threading.currentThread())
sys.stdout.flush() #this is essential to print before the thread is completed
You can also list all threads with a specific name
for at in threading.enumerate():
if at.getName().split(' ')[0] == 'Connection':
print('\t'+at.getName())
A similar thing can be done to a process.
Example process:
import multiprocessing
process1 = multiprocessing.Process(target=function,
name='ListenerProcess',
args=(queue, connections, known_clients, readouts),
daemon=True)
process1.start()
With processes it is even better as you can terminate a specific process by its name from outside of it
for child in multiprocessing.active_children():
if child.name == 'ListenerProcess':
print('Terminating:', child, 'PID:', child.pid)
child.terminate()
child.join()