Re: [Pyobjc-dev] Receiving distributed notifications in different thread
Brought to you by:
ronaldoussoren
From: Gerard T. <gto...@gm...> - 2014-10-06 18:12:52
|
Hi Robert, Not a problem at all, I'm always open to suggestions. We do actually have a redis server running, but that's to communicate regional or global events from a number of servers. Also, the project doesn't mandate that that redis process is running, so it's not critical in the system. It's only used to give the system overall a slightly more realtime nature. In this case, the webserver always runs on the same machine where the hardware events are occurring. For that reason, I was looking for a more direct way of communicating to the webserver without creating this dependency on redis. However, I'm not sure if I can get the proxy to work that way because of the way how threads and runloops of osx work. So I tried to implement what's going on here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Articles/Threading.html In the 'setUpThreadingSupport" function, it hangs on the [[NSRunLoop currentRunLoop] addPort:self.notificationPort forMode:(NSString __bridge *)kCFRunLoopCommonModes]; call and CPU goes to 100%. The "self" object is an NSObject. My assumption is that I am looking at the right way to enable threaded reception of distributed notifications and handle them in that code. The main thread of the webserver is controlled by django and doesn't eventually call the "control loop", which could be another reason this doesn't work well. Is this something that ought to work anyway, or would you recommend, given the rest of the architecture, to do this another way? Rgds, Gerard On Mon, Oct 6, 2014 at 2:18 PM, Robert Klep <rob...@gm...> wrote: > Hoi Gerard, > > I certainly don't want to berate your project, but just wondering: > wouldn't it be much easier to use something like Redis (or a full-blown MQ > broker) to distribute notifications? > > – robert > > Gerard Toonstra <gto...@gm...> wrote on Mon Oct 06 2014 at > 18:08:47: > > 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? > > > > |