コード例 #1
0
ファイル: eventloop.py プロジェクト: stjordanis/rubicon-objc
class EventLoopPolicy(events.AbstractEventLoopPolicy):
    """Rubicon event loop policy
    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.
    """
    def __init__(self):
        self._lifecycle = None
        self._default_loop = None
        self._watcher_lock = threading.Lock()
        self._watcher = None
        self._policy = DefaultEventLoopPolicy()
        self._policy.new_event_loop = self.new_event_loop
        self.get_event_loop = self._policy.get_event_loop
        self.set_event_loop = self._policy.set_event_loop

    def new_event_loop(self):
        """Create a new event loop and return it."""
        if not self._default_loop and threading.current_thread() == threading.main_thread():
            loop = self.get_default_loop()
        else:
            loop = CFEventLoop(self._lifecycle)
        loop._policy = self

        return loop

    def get_default_loop(self):
        """Get the default event loop."""
        if not self._default_loop:
            self._default_loop = self._new_default_loop()
        return self._default_loop

    def _new_default_loop(self):
        loop = CFEventLoop(self._lifecycle)
        loop._policy = self
        return loop

    def _init_watcher(self):
        with events._lock:
            if self._watcher is None:  # pragma: no branch
                self._watcher = SafeChildWatcher()
                if threading.current_thread() == threading.main_thread():
                    self._watcher.attach_loop(self._default_loop)

    def get_child_watcher(self):
        """Get the watcher for child processes.

        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 watcher for child processes."""
        if self._watcher is not None:
            self._watcher.close()

        self._watcher = watcher
コード例 #2
0
ファイル: eventloop.py プロジェクト: stjordanis/rubicon-objc
 def _init_watcher(self):
     with events._lock:
         if self._watcher is None:  # pragma: no branch
             self._watcher = SafeChildWatcher()
             if threading.current_thread() == threading.main_thread():
                 self._watcher.attach_loop(self._default_loop)