def run(self, sleep=0, log=True, __mode=None, __socket=None): if not __mode == "T" and current_thread().getName() == "MainThread": if os.name == "posix": _registerSignalHandler(SIGHUP, self._signalHandler) _registerSignalHandler(SIGINT, self._signalHandler) _registerSignalHandler(SIGTERM, self._signalHandler) if __socket is not None: from circuits.core.bridge import Bridge manager = Manager() bridge = Bridge(manager, socket=__socket) self.register(manager) manager.start() self._running = True self.fire(Started(self, __mode)) try: while self or self.running: self.tick() if sleep: time.sleep(sleep) finally: if __socket is not None: while bridge or manager: pass manager.stop() bridge.stop()
def start(self, process=False, link=None): """ Start a new thread or process that invokes this manager's ``run()`` method. The invocation of this method returns immediately after the task or process has been started. """ if process: # Parent<->Child Bridge if link is not None: from circuits.net.sockets import Pipe from circuits.core.bridge import Bridge channels = (uuid(), ) * 2 parent, child = Pipe(*channels) bridge = Bridge(parent, channel=channels[0]).register(link) args = (child, ) else: args = () bridge = None self.__process = Process(target=self.run, args=args, name=self.name) self.__process.daemon = True self.__process.start() return self.__process, bridge else: self.__thread = Thread(target=self.run, name=self.name) self.__thread.daemon = True self.__thread.start() return self.__thread, None
def start(self, sleep=0, log=True, link=None, process=False): group = None target = self.run name = self.__class__.__name__ mode = "P" if process else "T" args = ( sleep, log, mode, ) if process and HAS_MULTIPROCESSING: if link is not None and isinstance(link, Manager): from circuits.net.sockets import Pipe from circuits.core.bridge import Bridge from circuits.core.utils import findroot root = findroot(link) parent, child = Pipe() self._bridge = Bridge(root, socket=parent) self._bridge.start() args += (child, ) self._task = Process(group, target, name, args) self._task.daemon = True if HAS_MULTIPROCESSING == 2: setattr(self._task, "isAlive", self._task.is_alive) self.tick() self._task.start() return self._task = Thread(group, target, name, args) self._task.setDaemon(True) self._task.start()
def run(self, socket=None): """ Run this manager. The method fires the :class:`~.events.Started` event and then continuously calls :meth:`~.tick`. The method returns when the manager's :meth:`~.stop` method is invoked. If invoked by a programs main thread, a signal handler for the ``INT`` and ``TERM`` signals is installed. This handler fires the corresponding :class:`~.events.Signal` events and then calls :meth:`~.stop` for the manager. """ atexit.register(self.stop) if current_thread().getName() == "MainThread": try: set_signal_handler(SIGINT, self._signal_handler) set_signal_handler(SIGTERM, self._signal_handler) except ValueError: # Ignore if we can't install signal handlers pass self._running = True self.root._executing_thread = current_thread() # Setup Communications Bridge if socket is not None: from circuits.core.bridge import Bridge Bridge(socket, channel=socket.channel).register(self) self.fire(started(self)) try: while self.running or len(self._queue): self.tick() # Fading out, handle remaining work from stop event for _ in range(3): self.tick() except Exception as exc: if stderr: stderr.write("Unhandled ERROR: {0:s}\n".format(exc)) stderr.write(format_exc()) finally: try: self.tick() except: pass self.root._executing_thread = None self.__thread = None self.__process = None