[Pythonmac-SIG] NSDistributedNotificationCenter

Ronald Oussoren ronaldoussoren at mac.com
Fri Nov 9 06:32:35 CET 2007


On 8 Nov, 2007, at 20:55, Mike Covill wrote:

> I wanted to try using the NSDistributedNotificationCenter to pass
> messages between two separate python programs running simultaneously,
> so to learn about doing this I created this class:
>
> FILE: listener.py
> ------------------------------------------------------------------------
> -------------------------
> import Foundation
>
> class GetNotes(object):
>
>     def __init__(self):
>         '''register for an NSNotification'''
>         nc =  
> Foundation.NSDistributedNotificationCenter.defaultCenter()
>         nc.addObserver_selector_name_object_(self, 'getMyNotes:',
> 'myNote', None)
>
>     def getMyNotes_(self, note):
>         print 'got a note!:', note.object
> ------------------------------------------------------------------------
> -------------------------
>
> then I created an instance of this class and tried sending a
> notification via the command line, expecting to see a print statement
> that the notification was received:
>
> import Foundation as F
> import listener
> gn = listener.GetNotes()
> nc = F.NSDistributedNotificationCenter.defaultCenter()
> nc.postNotificationName_object_('myNote', None)
>
> So far, no such luck.  Any suggestions?

The delivery of notifications uses the runloop, it is basically just  
another source of events.

In a command-line tool you'll have to run the loop yourself, something  
like:

   loop = F.NSRunLoop.currentRunLoop()
   loop.run()

(That last call will run the eventloop and therefore "block" your  
script. It will also cause the notification to be delivered).

Ronald
>
>
> Thanks,
> Mike
> _______________________________________________
> Pythonmac-SIG maillist  -  Pythonmac-SIG at python.org
> http://mail.python.org/mailman/listinfo/pythonmac-sig

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2224 bytes
Desc: not available
Url : http://mail.python.org/pipermail/pythonmac-sig/attachments/20071109/66829461/attachment.bin 


More information about the Pythonmac-SIG mailing list