Is there a way to set title/name of a thread in Python?

mozcelikors picture mozcelikors · Jun 27, 2017 · Viewed 9.6k times · Source

I would like to set the title of a thread (the title seen in ps or top)in Python in order to make it visible to process tracer programs. All the threads of a process are always called python or called file name when /usr/bin/python is used and the script is called via ./script.

Now, I want to change each thread's name. I have a simple script with 4 threads (incl main thread). I use threading to start the threads.

Is there a way that I could achieve this without having to install third-party stuff? Any guidance is appreciated.

Answer

Nicolas Iceberg picture Nicolas Iceberg · Apr 30, 2020

Try this:

def your_function(arg1, arg2, argn):
    * do stuff *


new_thread = threading.Thread(target=your_function, args=(arg1, arg2, argn))
new_thread.name = 'your name'
new.thread.start()

Where new_thread.name is your answer.