def init( module, handler = None, shutdown = None, **options ): """ Init the notifier. """ if module == 'twisted': loop = TwistedLoop() elif module == 'thread': if not handler: raise RuntimeError('no callback handler provided') loop = ThreadLoop(handler, shutdown) else: raise RuntimeError('unknown notifier module %s', module) notifier.init( 'generic', force_internal=True, **options ) if shutdown is not None: # set specific shutdown function notifier.shutdown = shutdown # set main thread and init thread pipe main.set_as_mainthread() # adding a timer or socket is not thread safe in general but # an additional wakeup we don't need does not hurt. And in # simulation mode the step function does not modify the # internal variables. notifier.timer_add = Wakeup(loop, notifier.timer_add) notifier.socket_add = Wakeup(loop, notifier.socket_add) loop.start() return loop
def select_notifier(module, **options): """ Initialize the specified mainloop. :param module: the mainloop implementation to use. ``"generic"``: Python based mainloop, default; ``"gtk"``: pygtk mainloop; ``"threaded"``: Python based mainloop in an extra thread; ``"twisted"``: Twisted mainloop :type module: str :param options: module-specific keyword arguments """ if module in ('thread', 'twisted'): import nf_thread return nf_thread.init(module, **options) return notifier.init( module, **options )