def run(self): coloredlogs.install(getattr(logging, self.config.log_level, None), stream=sys.stderr) logger.info("Welcome to elpizo!") logger.info("Using event loop: %s", type(self.loop).__name__) self.start_event = asyncio.Event() self.loop.run_until_complete(green.coroutine(self.start)()) try: self.loop.run_forever() finally: self.loop.run_until_complete(green.coroutine(self.on_stop)())
def listen(self, port, host): logger.info("Server listening on %s:%s.", host, port) self.store.lock() green.await_coro( websockets.serve(green.coroutine(self.accept), host, port))
def once(self, _f, *args, **kwargs): def _wrapper(): green.await_coro(self.start_event.wait()) try: _f(self, *args, **kwargs) finally: self.loop.stop() asyncio.async(green.coroutine(_wrapper)(), loop=self.loop) self.run()
def once(self, _f, *args, **kwargs): def _wrapper(): green.await_coro(self.start_event.wait()) try: _f(self, *args, **kwargs) finally: self.loop.stop() asyncio. async (green.coroutine(_wrapper)(), loop=self.loop) self.run()
def do(self, _f, *args, **kwargs): fut = futures.Future() g_self = None def _wrapper(): nonlocal g_self g_self = greenlet.getcurrent() try: result = _f(*args, **kwargs) except BaseException as e: fut.set_exception(e) else: fut.set_result(result) self.loop.call_soon_threadsafe(asyncio.async, green.coroutine(_wrapper)()) try: return fut.result() except BaseException as e: self.loop.call_soon_threadsafe(g_self.throw, e) raise
def do(self, _f, *args, **kwargs): fut = futures.Future() g_self = None def _wrapper(): nonlocal g_self g_self = greenlet.getcurrent() try: result = _f(*args, **kwargs) except BaseException as e: fut.set_exception(e) else: fut.set_result(result) self.loop.call_soon_threadsafe( asyncio.async, green.coroutine(_wrapper)()) try: return fut.result() except BaseException as e: self.loop.call_soon_threadsafe(g_self.throw, e) raise
def broadcast(self, bus, channel, message): return asyncio.gather(*[ green.coroutine(self.send)(protocol, message) for protocol in bus.get_protocols_for_channel(channel) if protocol.policy.can_receive_broadcasts_from(self) ])
def send_via_bus(self, bus, target, message): return asyncio.gather(*[ green.coroutine(self.send)(protocol, message) for protocol in bus.get_protocols_for_channel(target.channel) ])
def start_behavior(self, behavior): logger.info("Starting behavior for NPC %s.", behavior.npc.id) self.npcs[behavior.npc.id] = behavior asyncio. async (green.coroutine(behavior.run)(), loop=self.loop)
def broadcast(self, bus, channel, message): return asyncio.gather(*[ green.coroutine(self.send)(protocol, message) for protocol in bus.get_protocols_for_channel(channel) if protocol.policy.can_receive_broadcasts_from(self)])
def send_via_bus(self, bus, target, message): return asyncio.gather(*[ green.coroutine(self.send)(protocol, message) for protocol in bus.get_protocols_for_channel(target.channel)])
def route(self, path, handler): self.app.route(path)(green.coroutine(handler))
def start_behavior(self, behavior): logger.info("Starting behavior for NPC %s.", behavior.npc.id) self.npcs[behavior.npc.id] = behavior asyncio.async(green.coroutine(behavior.run)(), loop=self.loop)