Does Python have any kind of 'messaging'?

J

j_mckitrick

I used to like Objective-C and Gnustep, but I gave up for lack of
support and recognition of the language/platform under Unix. But it
did offer NSNotifications, a basic message class that could be posted
and received by all subscribers. Other than global variables used to
share data across threads, does Python have anything similar?

jonathon
 
J

Josiah Carlson

I used to like Objective-C and Gnustep, but I gave up for lack of
support and recognition of the language/platform under Unix. But it
did offer NSNotifications, a basic message class that could be posted
and received by all subscribers. Other than global variables used to
share data across threads, does Python have anything similar?

No, but you can write one yourself quite easily, depending on the
semantics of your messaging system.

- Josiah


import threading

channels = {}
lock = threading.RLock()

def subscribe(id, funct, channel):
lock.acquire()
if channel in channels:
channels[channel][id] = funct
else:
channels[channel] = {id:funct}
lock.release()

def unsubscribe(id, channel):
lock.acquire()
if channel in channels:
try:
del channels[channel][id]
except KeyError:
pass
lock.release()

def post(channel, message):
lock.acquire()
if channel in channels:
for funct in channels[channel].itervalues():
funct(message)
lock.release()
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top