Title: threading.Thread always gets name "Thread" instead of a discriminating one (patch included)
Type: behaviour Severity: normal
Components: Library Versions: 2.5.2
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: amak Nosy List: amak, irmen
Priority: Keywords: patch

Created on 2011-10-09.22:57:59 by irmen, last changed 2012-04-01.16:11:48 by amak.

File name Uploaded Description Edit Remove
threading.patch irmen, 2011-11-06.22:41:39 patch for
msg6665 (view) Author: Irmen de Jong (irmen) Date: 2011-10-09.22:57:59
threads created with threading.Thread() always get a default name "Thread" instead of a discriminating one.

CPython for instance appends a sequencenumber or thread id so we can discriminate threads easily ("Thread-1", "Thread-2", ...).
msg6710 (view) Author: Irmen de Jong (irmen) Date: 2011-11-06.22:41:39
Added simple patch.
I've added the _newname function to (mirorred from cpython)

Threads now get named Thread-1, Thread-2, ... as they are created, so they have discriminating names by default.
msg7004 (view) Author: Alan Kennedy (amak) Date: 2012-03-31.20:48:45
Is it important for the name to be based on an incrementing counter.

I have a working change that fixes this issue. I initially used an integer counter, incrementing by one each time. But there is a remote possibility of a clash in thread names with this scenario, since the counter wraps around at 2**31.

I'd prefer to use the hashCode of the thread object itself, which will lower the probability of clashes.

So thread names would look like this

>>> from threading import Thread as t
>>> t().getName()
>>> t().getName()
>>> t().getName()
>>> t().getName()
>>> t().getName()
>>> t().getName()
>>> t().getName()
>>> t().getName()
>>> t().getName()
msg7006 (view) Author: Irmen de Jong (irmen) Date: 2012-03-31.21:04:03
Nah, it's just what CPython does in its
The docs say "By default, a unique name is constructed of the form “Thread-N” where N is a small decimal number." So as long as the name is unique, and N is a number, we should be fine.
msg7010 (view) Author: Alan Kennedy (amak) Date: 2012-04-01.16:11:48


I decided to go with incrementing integers instead of object hashCode()s, since the probability of clashes is lower with incrementing integers.
Date User Action Args
2012-04-01 16:11:48amaksetstatus: open -> closed
resolution: fixed
messages: + msg7010
2012-03-31 21:04:03irmensetmessages: + msg7006
2012-03-31 20:48:58amaksetassignee: amak
2012-03-31 20:48:45amaksetnosy: + amak
messages: + msg7004
2011-11-06 22:41:40irmensetfiles: + threading.patch
keywords: + patch
messages: + msg6710
title: threading.Thread always gets name "Thread" instead of a discriminating one -> threading.Thread always gets name "Thread" instead of a discriminating one (patch included)
2011-10-09 22:57:59irmencreate