Issue1805

classification
Title: threading.Thread always gets name "Thread" instead of a discriminating one (patch included)
Type: behaviour Severity: normal
Components: Library Versions: 2.5.2
process
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.

Files
File name Uploaded Description Edit Remove
threading.patch irmen, 2011-11-06.22:41:39 patch for threading.py
Messages
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 threading.py (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()
u'Thread-14662467'
>>> t().getName()
u'Thread-6815360'
>>> t().getName()
u'Thread-8640702'
>>> t().getName()
u'Thread-20730792'
>>> t().getName()
u'Thread-32316862'
>>> t().getName()
u'Thread-31983457'
>>> t().getName()
u'Thread-29881213'
>>> t().getName()
u'Thread-27649674'
>>> t().getName()
u'Thread-10545552'
msg7006 (view) Author: Irmen de Jong (irmen) Date: 2012-03-31.21:04:03
Nah, it's just what CPython does in its threading.py
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
Fixed.

2.5:  http://hg.python.org/jython/rev/920a60f5d5b5
head: http://hg.python.org/jython/rev/22450f53ce82

I decided to go with incrementing integers instead of object hashCode()s, since the probability of clashes is lower with incrementing integers.
History
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