By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

threading id generator ?

P: n/a
Hi all,

I'm a python newbie with a little problem; my need is to share an incrementing counter between threads; i.e. every time I create a new thread (which handle a single tcp/ip connection), I must assign an unique numeric ID to it, without reuse old numbers... In example:

thread-1 id: 1
thread-2 id: 2
thread-3 id: 3
thread-100 id: 100
thread-n id: n

which locking semantic I must use to implement a shared generator in a thread ?

thanks in advance :)
Questo messaggio di posta elettronica contiene informazioni di carattere confidenziale rivolte esclusivamente al destinatario sopra indicato.
E' vietato l'uso, la diffusione, distribuzione o riproduzione da parte di ogni altra persona.
Nel caso aveste ricevuto questo messaggio di posta elettronica per errore, siete pregati di segnalarlo immediatamente al mittentee distruggere quanto ricevuto (compresi file allegati) senza farne copia.
Qualsivoglia utilizzo non autorizzato del contenuto di questo messaggio costituisce violazione dell'obbligo di non prendere cognizione della corrispondenza tra gli altri soggetti, salvo piu grave illecito, ed espone il responsabile alle relative conseguenze.

Confidentiality Notice. This electronic mail transmission may contain legally priviledge and/or confidential information. Do not read this if you are not the person(s) named.
Any use, distribution, copying or disclosure by any other person is stricly prohibited.
If you received this trasmission in error, please notify the sender and destroy the original transmission and its attachments without reading or saving in any manner.
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> which locking semantic I must use to implement a shared generator in a
thread ?

thanks in advance :)


You could try something like this:

class IdGen:
def __init__(_):
_.lock = threading.Lock()
_.id = 0

def id_gen(_):
_.lock.aqcuire()
new_id = _.id
_.id += 1
lock.release()
return new_id
An instance of IdGen you could store in a global variable. Or you make it a
singleton. Or you pass one instance around for all your threading-objects.
Or you make _.id and _.lock class-variables, and inherit all your
threading-objects from IdGen. Or you make it a module.... and so on :)

--
Regards,

Diez B. Roggisch

Jul 18 '05 #2

P: n/a
[Andrea Manzini]
I'm a python newbie with a little problem; my need is to share an
incrementing counter between threads; i.e. every time I create a
new thread (which handle a single tcp/ip connection), I must assign
an unique numeric ID to it, without reuse old numbers... In example:

thread-1 id: 1
thread-2 id: 2
thread-3 id: 3
thread-100 id: 100
thread-n id: n

which locking semantic I must use to implement a shared generator
in a thread ?


This came up about a month ago. Group-Googling for ("shared generator"
and "multiple threads") gives useful results.

http://groups.google.com/groups?hl=e....lang.python.*

regards,

--
alan kennedy
------------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/contact/alan
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.