[Pyobjc-dev] Receiving distributed notifications in different thread
Brought to you by:
ronaldoussoren
|
From: Gerard T. <gto...@gm...> - 2014-10-06 16:03:51
|
Hello,
I'm new to pyobjc. We're using it for a project to receive events from a
command line tool in C which interfaces to drivers to generate events on
hardware changes.
There's another process coded in 100% python where we want to receive such
notifications and use them. Unfortunately, we haven't had much luck so far.
This 100% python process is a webserver, specifically django. In the
startup code, we want to create a class (singleton) that creates a separate
thread to deal with these distributed notifications.
For a small test script as a proof of concept, the runConsoleEventLoop is
called from the main thread and everything works. Apparently because each
thread has or should have its own input sources and the main thread is
responsible for those. Creating a new thread requires the programmer to add
their own. Not doing that, the loop immediately returns as the "nextfire"
variable will be "None".
We've been struggling to add our own input source, specifically one that
deals with distributed notifications, to this runloop. When we call the
following code:
self.notifications = NSMutableArray.alloc().init()
self.notiticationLock = NSLock.alloc().init()
self.notificationThread = NSThread.currentThread()
self.notificationPort = NSMachPort.alloc().init()
self.notificationPort.setDelegate_(self)
runLoop = NSRunLoop.currentRunLoop()
runLoop.addPort_forMode_( self.notificationPort,
kCFRunLoopCommonModes )
It hangs on the last line "addPort_forMode_" and so we never get to the
loop section. I think that's a bug, because the same code is used by an
example from Apple, where they'd call run() or some other equivalent to
start processing those events in the run loop.
What are we doing wrong? does anyone know an example that demonstrates
this?
|