473,480 Members | 4,282 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

threading id generator ?

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
2 1879
> 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
[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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2666
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
17
2404
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next...
1
5801
by: Gernot Hillier | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi! I'm the developer of a Linux ISDN application which uses embedded Python for controlling the communication. It starts several threads (i.e....
45
2976
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
11
1803
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using...
10
2154
by: Janto Dreijer | last post by:
I have been having problems with the Python 2.4 and 2.5 interpreters on both Linux and Windows crashing on me. Unfortunately it's rather complex code and difficult to pin down the source. So...
14
3416
by: Akihiro KAYAMA | last post by:
Hi all. I found cooperative multi-threading(only one thread runs at once, explicit thread switching) is useful for writing some simulators. With it, I'm able to be free from annoying mutual...
10
4391
by: George Sakkis | last post by:
I'd like some feedback on a solution to a variant of the producer- consumer problem. My first few attempts turned out to deadlock occasionally; this one seems to be deadlock-free so far but I can't...
1
1743
by: Bruce Frederiksen | last post by:
I wanted to make everybody aware that I've posted a (rather long and involved) PEP proposal for adding micro-threading to Python on python-ideas for feedback and review. In a nutshell, this...
0
7039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7037
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2992
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.