diff options
author | Devaev Maxim <[email protected]> | 2020-03-01 20:50:50 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2020-03-01 20:50:50 +0300 |
commit | e855976f05775359b774c3d5d6c3a6f6532efce6 (patch) | |
tree | b1fcfdd386cb01f59768e6d7dc3e7f84ec18edcd /kvmd/aiomulti.py | |
parent | 44b0ab19bf33a2bdd0d9637f14128f968cf1764e (diff) |
no pid limits
Diffstat (limited to 'kvmd/aiomulti.py')
-rw-r--r-- | kvmd/aiomulti.py | 30 |
1 files changed, 9 insertions, 21 deletions
diff --git a/kvmd/aiomulti.py b/kvmd/aiomulti.py index 04eb45e1..fc3724dd 100644 --- a/kvmd/aiomulti.py +++ b/kvmd/aiomulti.py @@ -20,7 +20,6 @@ # ========================================================================== # -import os import multiprocessing import multiprocessing.queues import multiprocessing.sharedctypes @@ -35,14 +34,11 @@ from . import aiotools class AioProcessNotifier: def __init__(self) -> None: self.__queue: multiprocessing.queues.Queue = multiprocessing.Queue() - self.__pid = os.getpid() def notify(self) -> None: - assert os.getpid() != self.__pid, "Child only" self.__queue.put(None) async def wait(self) -> None: - assert os.getpid() == self.__pid, "Parent only" while not (await aiotools.run_async(self.__inner_wait)): pass @@ -64,40 +60,32 @@ class AioSharedFlags: notifier: AioProcessNotifier, ) -> None: - self.__local_flags = dict(initial) # To fast comparsion self.__notifier = notifier - self.__shared_flags: Dict[str, multiprocessing.sharedctypes.RawValue] = { + self.__flags: Dict[str, multiprocessing.sharedctypes.RawValue] = { key: multiprocessing.RawValue("i", int(value)) # type: ignore for (key, value) in initial.items() } + self.__lock = multiprocessing.Lock() - self.__pid = os.getpid() def update(self, **kwargs: bool) -> None: - assert os.getpid() != self.__pid, "Child only" changed = False - try: + with self.__lock: for (key, value) in kwargs.items(): - value = bool(value) - if self.__local_flags[key] != value: - if not changed: - self.__lock.acquire() - self.__shared_flags[key].value = int(value) - self.__local_flags[key] = value + value = int(value) # type: ignore + if self.__flags[key].value != value: + self.__flags[key].value = value changed = True - finally: - if changed: - self.__lock.release() - self.__notifier.notify() + if changed: + self.__notifier.notify() async def get(self) -> Dict[str, bool]: return (await aiotools.run_async(self.__inner_get)) def __inner_get(self) -> Dict[str, bool]: - assert os.getpid() == self.__pid, "Parent only" with self.__lock: return { key: bool(shared.value) - for (key, shared) in self.__shared_flags.items() + for (key, shared) in self.__flags.items() } |