threading id generator ?

A

Andrea Manzini

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.
 
D

Diez B. Roggisch

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 :)
 
A

Alan Kennedy

[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=...ltiple+threads"&meta=group=comp.lang.python.*

regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,811
Messages
2,569,693
Members
45,477
Latest member
IsidroSeli

Latest Threads

Top