Rework startup and Application-Engine relation

- Application and Engine will be started by __init__.run() independently
- Exiting app will disconnect clients
- Signals like SIGINT (Ctrl-C) are now handled for proper exit
This commit is contained in:
miruka
2019-05-01 01:23:38 -04:00
parent 5ad13aed7d
commit 12ce4cdb30
8 changed files with 72 additions and 55 deletions

View File

@@ -13,11 +13,6 @@ import nio
from .network_manager import NetworkManager
from .pyqt_future import futurize
# One pool per hostname/remote server;
# multiple Client for different accounts on the same server can exist.
_POOLS: DefaultDict[str, ThreadPoolExecutor] = \
DefaultDict(lambda: ThreadPoolExecutor(max_workers=6))
class Client(QObject):
roomInvited = pyqtSignal([str, dict], [str])
@@ -36,9 +31,9 @@ class Client(QObject):
def __init__(self,
manager,
hostname: str,
username: str,
device_id: str = "") -> None:
hostname: str,
username: str,
device_id: str = "") -> None:
super().__init__(manager)
self.manager = manager
@@ -46,7 +41,7 @@ class Client(QObject):
self.host: str = host
self.port: int = int(port[0]) if port else 443
self.pool: ThreadPoolExecutor = _POOLS[self.host]
self.pool: ThreadPoolExecutor = ThreadPoolExecutor(6)
self.nio: nio.client.HttpClient = \
nio.client.HttpClient(self.host, username, device_id)
@@ -109,9 +104,12 @@ class Client(QObject):
@futurize(pyqt=False)
def startSyncing(self) -> None:
while True:
self._on_sync(self.net_sync.talk(
self.nio_sync.sync, timeout=8000
))
try:
response = self.net_sync.talk(self.nio_sync.sync, timeout=8000)
except nio.LocalProtocolError: # logout occured
pass
else:
self._on_sync(response)
if self._stop_sync.is_set():
self._stop_sync.clear()

View File

@@ -85,6 +85,13 @@ class ClientManager(QObject):
client.logout()
@pyqtSlot()
def deleteAll(self) -> None:
for user_id in self.clients.copy():
self.delete(user_id)
print("deleted", user_id, self.clients)
@pyqtProperty(str, constant=True)
def defaultDeviceName(self) -> str: # pylint: disable=no-self-use
os_ = f" on {platform.system()}".rstrip()

View File

@@ -80,7 +80,7 @@ def futurize(max_instances: Optional[int] = None, pyqt: bool = True
return func(self, *args, **kws)
except Exception:
traceback.print_exc()
logging.error("Exiting thread due to exception.")
logging.error("Exiting thread/process due to exception.")
sys.exit(1)
finally:
del _RUNNING[_RUNNING.index((self.pool, func, args, kws))]