PKSLT7* * redis_relay.py"redis relay for interprocess communication" import os import uuid import base64 import pickle import asyncio import weakref import logging import inspect __version__ = "0.0.1" logger = logging.getLogger(__name__) def get_id(): ident = (uuid.getnode() << 16) + os.getpid() ident = base64.urlsafe_b64encode(ident.to_bytes(8, "big")) ident = ident.decode() return ident def get_id2(): return uuid.uuid4().hex async def lock_run(coro, lock): async with lock: return await coro class RedisRelay: ident_hkey = "ident-channel-map" def __init__(self, redis, loop): self.redis = redis self.channel_id = "ch:{}".format(get_id()) self.tasks = set() self.handlers = weakref.WeakValueDictionary() self.main_task = loop.create_task(self.run(loop)) async def run(self, loop): channel, = await self.redis.subscribe(self.channel_id) self.ch = channel async for data in channel.iter(): try: message = pickle.loads(data) ident, method, args, kw = message handler = self.handlers[ident] coro = getattr(handler, "on_" + method)(*args, **kw) if inspect.isawaitable(coro): if hasattr(handler, "lock"): coro = lock_run(coro, handler.lock) task = loop.create_task(coro) self.tasks.add(task) task.add_done_callback(self.tasks.remove) except Exception: logger.exception("channel data handling error") async def stop(self): try: await self.redis.unsubscribe(self.channel_id) finally: self.main_task.cancel() if self.tasks: await asyncio.wait(self.tasks) async def login(self, ident: str, handler) -> None: await self.redis.hset(self.ident_hkey, ident, self.channel_id) self.handlers[ident] = handler async def logout(self, ident: str) -> None: await self.redis.hdel(self.ident_hkey, ident) self.handlers.pop(ident, None) def __getattr__(self, name: str): if not name.startswith("call_"): raise AttributeError method = name[5:] async def func(ident, *args, **kw): channel_id = await self.redis.hget(self.ident_hkey, ident) if not channel_id: return False data = pickle.dumps([ident, method, args, kw]) await self.redis.publish(channel_id, data) return func PKL **#redis_relay-0.0.1.dist-info/LICENSEMIT License Copyright (c) 2018 Yingbo Gu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HNO!redis_relay-0.0.1.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,zd&Y)r$[)T&UrPK!H$redis_relay-0.0.1.dist-info/METADATA_o0)xI" (v&ֱsftBՇVs]{xƙgwN&җd.P}|`b4ЕT6h ,G+$ L7.2!}ulDKYBm~1_ .L5ۄ…>~I: EWK%RWIKLg$kۭ7*Sӧ޹K/\{,\[~ȪKz?>Rjn7 D䌳zdpoM )Q#B$PK!H;a}"redis_relay-0.0.1.dist-info/RECORD}̻v0o 4I)("R`FѯoN7ƻ\= vGkm7ueV*fos\4ؤ]ΘU⿑ e$![nMdyd1j\N֏;SڵCw:} qqe9s~&͍!i(Ж/%a1"E@hP*TOBA[t)Xb /PKSLT7* * redis_relay.pyPKL **#V redis_relay-0.0.1.dist-info/LICENSEPK!HNO!redis_relay-0.0.1.dist-info/WHEELPK!H$Nredis_relay-0.0.1.dist-info/METADATAPK!H;a}"redis_relay-0.0.1.dist-info/RECORDPK~