How to set the name of a QThread in pyqt? How to set the name of a QThread in pyqt? multithreading multithreading

How to set the name of a QThread in pyqt?


If the threading module is available, the logging module will use threading.current_thread().name to set the threadName LogRecord attribute.

But the docs for threading.current_thread say that a dummy thread object will be used if the current thread was not created by the threading module (hence the "Dummy-x" name).

I suppose it would be possible to monkey-patch threading.current_thread to reset the name to something more appropriate. But surely a much better approach would be to make use of the extra dictionary when logging a message:

logging.Formatter('%(levelname)-8s %(asctime)s %(qthreadname)-15s %(message)s')...extras = {'qthreadname': get_qthreadname()}logging.warning(message, extra=extras)


From the Qt5 documentation, you can call setObjectName() to modify the thread name

To choose the name that your thread will be given (as identified by the command ps -L on Linux, for example), you can call setObjectName() before starting the thread.

If you don't call setObjectName(), the name given to your thread will be the class name of the runtime type of your thread object (for example, "RenderThread" in the case of the Mandelbrot Example, as that is the name of the QThread subclass).

Unfortunately, it also notes:

this is currently not available with release builds on Windows.