def __init__(self): super().__init__() self._loop = None # type: asyncio.AbstractEventLoop self._control = ControlManager() self._control_server = ControlServer(self._control, None) self._stopping = False
def status_server_handler(manager: ControlManager) -> str: torrents = manager.get_torrents() if not torrents: return 'No torrents added' torrents.sort(key=lambda info: info.download_info.suggested_name) return [TorrentState(torrent_info) for torrent_info in torrents]
def run_daemon(_): with closing(asyncio.get_event_loop()) as loop: loop.run_until_complete(check_daemon_absence()) control = ControlManager() loop.run_until_complete(control.start()) try: control.load_state() except Exception as err: logging.warning('Failed to load program state: %r', err) control.invoke_state_dumps() stopping = False def stop_daemon(server: ControlServer): nonlocal stopping if stopping: return stopping = True stop_task = asyncio.ensure_future(asyncio.wait([server.stop(), server.control.stop()])) stop_task.add_done_callback(lambda fut: loop.stop()) control_server = ControlServer(control, stop_daemon) loop.run_until_complete(control_server.start()) if os.name == 'posix': for sig in (signal.SIGINT, signal.SIGTERM): loop.add_signal_handler(sig, partial(stop_daemon, control_server)) loop.run_forever()
class ControlManagerThread(QThread): error_happened = pyqtSignal(str, Exception) def __init__(self): super().__init__() self._loop = None # type: asyncio.AbstractEventLoop self._control = ControlManager() self._control_server = ControlServer(self._control, None) self._stopping = False @property def loop(self) -> asyncio.AbstractEventLoop: return self._loop @property def control(self) -> ControlManager: return self._control def run(self): self._loop = asyncio.new_event_loop() asyncio.set_event_loop(self._loop) with closing(self._loop): self._loop.run_until_complete(self._control.start()) self._loop.run_until_complete(self._control_server.start()) try: self._control.load_state() except Exception as err: self.error_happened.emit('Failed to load program state', err) self._control.invoke_state_dumps() self._loop.run_forever() def stop(self): if self._stopping: return self._stopping = True stop_fut = asyncio.run_coroutine_threadsafe(asyncio.wait([self._control_server.stop(), self._control.stop()]), self._loop) stop_fut.add_done_callback(lambda fut: self._loop.stop()) self.wait()
def status_server_handler(manager: ControlManager) -> List[TorrentState]: torrents = manager.get_torrents() torrents.sort(key=lambda info: info.download_info.suggested_name) return [TorrentState(torrent_info) for torrent_info in torrents]