def _init_watcher(self): with asyncio.events._lock: if self._watcher is None: # pragma: no branch self._watcher = SafeChildWatcher() if isinstance(threading.current_thread(), threading._MainThread): self._watcher.attach_loop(self._local._loop)
class ZmqEventLoopPolicy(asyncio.AbstractEventLoopPolicy): """ZeroMQ policy implementation for accessing the event loop. In this policy, each thread has its own event loop. However, we only automatically create an event loop by default for the main thread; other threads by default have no event loop. """ class _Local(threading.local): _loop = None _set_called = False def __init__(self): self._local = self._Local() self._watcher = None def get_event_loop(self): """Get the event loop. If current thread is the main thread and there are no registered event loop for current thread then the call creates new event loop and registers it. Return an instance of ZmqEventLoop. Raise RuntimeError if there is no registered event loop for current thread. """ if (self._local._loop is None and not self._local._set_called and isinstance( threading.current_thread(), threading._MainThread)): self.set_event_loop(self.new_event_loop()) assert self._local._loop is not None, \ ('There is no current event loop in thread %r.' % threading.current_thread().name) return self._local._loop def new_event_loop(self): """Create a new event loop. You must call set_event_loop() to make this the current event loop. """ return ZmqEventLoop() def set_event_loop(self, loop): """Set the event loop. As a side effect, if a child watcher was set before, then calling .set_event_loop() from the main thread will call .attach_loop(loop) on the child watcher. """ self._local._set_called = True assert loop is None or isinstance(loop, asyncio.AbstractEventLoop), \ "loop should be None or AbstractEventLoop instance" self._local._loop = loop if (self._watcher is not None and isinstance( threading.current_thread(), threading._MainThread)): self._watcher.attach_loop(loop) if sys.platform != 'win32': def _init_watcher(self): with asyncio.events._lock: if self._watcher is None: # pragma: no branch self._watcher = SafeChildWatcher() if isinstance(threading.current_thread(), threading._MainThread): self._watcher.attach_loop(self._local._loop) def get_child_watcher(self): """Get the child watcher. If not yet set, a SafeChildWatcher object is automatically created. """ if self._watcher is None: self._init_watcher() return self._watcher def set_child_watcher(self, watcher): """Set the child watcher.""" assert watcher is None or \ isinstance(watcher, asyncio.AbstractChildWatcher), \ "watcher should be None or AbstractChildWatcher instance" if self._watcher is not None: self._watcher.close() self._watcher = watcher
class ZmqEventLoopPolicy(asyncio.AbstractEventLoopPolicy): """ZeroMQ policy implementation for accessing the event loop. In this policy, each thread has its own event loop. However, we only automatically create an event loop by default for the main thread; other threads by default have no event loop. """ class _Local(threading.local): _loop = None _set_called = False def __init__(self): self._local = self._Local() self._watcher = None def get_event_loop(self): """Get the event loop. If current thread is the main thread and there are no registered event loop for current thread then the call creates new event loop and registers it. Return an instance of ZmqEventLoop. Raise RuntimeError if there is no registered event loop for current thread. """ if (self._local._loop is None and not self._local._set_called and isinstance(threading.current_thread(), threading._MainThread)): self.set_event_loop(self.new_event_loop()) assert self._local._loop is not None, \ ('There is no current event loop in thread %r.' % threading.current_thread().name) return self._local._loop def new_event_loop(self): """Create a new event loop. You must call set_event_loop() to make this the current event loop. """ return ZmqEventLoop() def set_event_loop(self, loop): """Set the event loop. As a side effect, if a child watcher was set before, then calling .set_event_loop() from the main thread will call .attach_loop(loop) on the child watcher. """ self._local._set_called = True assert loop is None or isinstance(loop, asyncio.AbstractEventLoop), \ "loop should be None or AbstractEventLoop instance" self._local._loop = loop if (self._watcher is not None and isinstance(threading.current_thread(), threading._MainThread)): self._watcher.attach_loop(loop) if sys.platform != 'win32': def _init_watcher(self): with asyncio.events._lock: if self._watcher is None: # pragma: no branch self._watcher = SafeChildWatcher() if isinstance(threading.current_thread(), threading._MainThread): self._watcher.attach_loop(self._local._loop) def get_child_watcher(self): """Get the child watcher. If not yet set, a SafeChildWatcher object is automatically created. """ if self._watcher is None: self._init_watcher() return self._watcher def set_child_watcher(self, watcher): """Set the child watcher.""" assert watcher is None or \ isinstance(watcher, asyncio.AbstractChildWatcher), \ "watcher should be None or AbstractChildWatcher instance" if self._watcher is not None: self._watcher.close() self._watcher = watcher