Example #1
0
    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)())
Example #2
0
  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)())
Example #3
0
  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))
Example #4
0
  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()
Example #5
0
    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()
Example #6
0
    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
Example #7
0
  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
Example #8
0
    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))
Example #9
0
 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)
     ])
Example #10
0
 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)
     ])
Example #11
0
 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)
Example #12
0
 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)])
Example #13
0
 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)])
Example #14
0
 def route(self, path, handler):
     self.app.route(path)(green.coroutine(handler))
Example #15
0
 def route(self, path, handler):
   self.app.route(path)(green.coroutine(handler))
Example #16
0
 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)