Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a python thread name from inside the thread on Windows?

I have a main thread that gets connections and immediately spawns new threads then inside the thread it receives a name that gets logged, currently my threads are named Thread-n. I would like to change the name of the thread from inside. I found this, but it sadly only works on linux, any similar program to do this on Windows?

like image 476
Hakaishin Avatar asked Jul 18 '18 08:07

Hakaishin


People also ask

How do you change a thread name in Python?

The name for a new thread can be set via the “name” argument in the constructor to the threading.

How does Python identify a thread?

Each thread also has an identifier. Python will assign each thread an integer identifier number. This can be retrieved via the get_ident() function. The operating system will also assign each thread a unique integer that can be retrieved by the get_native_id() function.

Can a Python thread start another thread?

Yes, spawning threads from within another thread is OK. Just remember that there exists a main Python thread, that governs the others.

What is a Daemonic thread Python?

The threads which are always going to run in the background that provides supports to main or non-daemon threads, those background executing threads are considered as Daemon Threads. The Daemon Thread does not block the main thread from exiting and continues to run in the background.


1 Answers

One can do this by using:

import threading
threading.current_thread().name = "my_name"
like image 132
Hakaishin Avatar answered Sep 22 '22 19:09

Hakaishin