Python asyncio.run_coroutine_threadsafe() Examples

The following are 30 code examples of asyncio.run_coroutine_threadsafe(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module asyncio , or try the search function .
Example #1
Source File: gsm.py    From pai with Eclipse Public License 2.0 6 votes vote down vote up
def handle_message(self, timestamp: str, source: str, message: str) -> None:
        """ Handle GSM message. It should be a command """

        logger.debug("Received: {} {} {}".format(
            timestamp, source, message))

        if source in cfg.GSM_CONTACTS:
            future = asyncio.run_coroutine_threadsafe(self.handle_command(message), self.alarm.work_loop)
            ret = future.result(10)

            m = "GSM {}: {}".format(source, ret)
            logger.info(m)
        else:
            m = "GSM {} (UNK): {}".format(source, message)
            logger.warning(m)

        self.send_message(m, EventLevel.INFO)
        ps.sendNotification(Notification(sender=self.name, message=m, level=EventLevel.INFO)) 
Example #2
Source File: peer_outer_service.py    From loopchain with Apache License 2.0 6 votes vote down vote up
def AnnounceUnconfirmedBlock(self, request, context):
        """Send the UnconfirmedBlock includes collected transactions to reps and request to verify it.

        :param request:
        :param context:
        :return:
        """
        channel_name = conf.LOOPCHAIN_DEFAULT_CHANNEL if request.channel == '' else request.channel
        channel_stub = StubCollection().channel_stubs[channel_name]

        try:
            round_ = request.round_
        except AttributeError:
            round_ = 0

        asyncio.run_coroutine_threadsafe(
            channel_stub.async_task().announce_unconfirmed_block(request.block, round_),
            self.peer_service.inner_service.loop
        )
        return loopchain_pb2.CommonReply(response_code=message_code.Response.success, message="success") 
Example #3
Source File: connection.py    From switchio with Mozilla Public License 2.0 6 votes vote down vote up
def run_in_order_threadsafe(awaitables, loop, timeout=0.5, block=True):
    """"Given a sequence of awaitables, schedule each threadsafe in order
    optionally blocking until completion.

    Returns a `concurrent.futures.Future` which can be used to wait on the
    result returned from the last awaitable. If `block` is `True` the final
    result will be waited on before returning control to the caller.
    """
    future = asyncio.run_coroutine_threadsafe(
        await_in_order(awaitables, loop, timeout),
        loop
    )

    if block:
        if not loop.is_running():
            result = loop.run_until_complete(
                asyncio.wrap_future(future, loop=loop))
            assert result is future.result()
        else:
            future.result(timeout)

    return future 
Example #4
Source File: peer_outer_service.py    From loopchain with Apache License 2.0 6 votes vote down vote up
def GetInvokeResult(self, request, context):
        """get invoke result by tx_hash

        :param request: request.tx_hash = tx_hash
        :param context:
        :return: verify result
        """
        channel_name = conf.LOOPCHAIN_DEFAULT_CHANNEL if request.channel == '' else request.channel
        utils.logger.debug(f"peer_outer_service:GetInvokeResult in channel({channel_name})")

        channel_stub = StubCollection().channel_stubs[channel_name]
        future = asyncio.run_coroutine_threadsafe(
            channel_stub.async_task().get_invoke_result(request.tx_hash),
            self.peer_service.inner_service.loop
        )
        response_code, result = future.result()

        return loopchain_pb2.GetInvokeResultReply(response_code=response_code, result=result) 
Example #5
Source File: peer_outer_service.py    From loopchain with Apache License 2.0 6 votes vote down vote up
def GetPrecommitBlock(self, request, context):
        """Return the precommit bock.

        :param request:
        :param context:
        :return: loopchain.proto 의 PrecommitBlockReply 참고,
        """

        channel_name = conf.LOOPCHAIN_DEFAULT_CHANNEL if request.channel == '' else request.channel
        channel_stub = StubCollection().channel_stubs[channel_name]
        future = asyncio.run_coroutine_threadsafe(
            channel_stub.async_task().get_precommit_block(last_block_height=request.last_block_height),
            self.peer_service.inner_service.loop
        )
        response_code, response_message, block = future.result()

        return loopchain_pb2.PrecommitBlockReply(
            response_code=response_code, response_message=response_message, block=block) 
Example #6
Source File: __init__.py    From ha-composite-tracker with The Unlicense 6 votes vote down vote up
def setup(hass, config):
    if (any(conf[CONF_TIME_AS] in (TZ_DEVICE_UTC, TZ_DEVICE_LOCAL)
           for conf in (config.get(DT_DOMAIN) or [])
           if conf[CONF_PLATFORM] == DOMAIN)):
        pkg = config[DOMAIN][CONF_TZ_FINDER]
        try:
            asyncio.run_coroutine_threadsafe(
                async_process_requirements(
                    hass, '{}.{}'.format(DOMAIN, DT_DOMAIN), [pkg]),
                hass.loop
            ).result()
        except RequirementsNotFound:
            _LOGGER.debug('Process requirements failed: %s', pkg)
            return False
        else:
            _LOGGER.debug('Process requirements suceeded: %s', pkg)

        if pkg.split('==')[0].strip().endswith('L'):
            from timezonefinderL import TimezoneFinder
        else:
            from timezonefinder import TimezoneFinder
        hass.data[DOMAIN] = TimezoneFinder()

    return True 
Example #7
Source File: test_tasks.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def target(self, fail=False, cancel=False, timeout=None,
               advance_coro=False):
        """Run add coroutine in the event loop."""
        coro = self.add(1, 2, fail=fail, cancel=cancel)
        future = asyncio.run_coroutine_threadsafe(coro, self.loop)
        if advance_coro:
            # this is for test_run_coroutine_threadsafe_task_factory_exception;
            # otherwise it spills errors and breaks **other** unittests, since
            # 'target' is interacting with threads.

            # With this call, `coro` will be advanced, so that
            # CoroWrapper.__del__ won't do anything when asyncio tests run
            # in debug mode.
            self.loop.call_soon_threadsafe(coro.send, None)
        try:
            return future.result(timeout)
        finally:
            future.done() or future.cancel() 
Example #8
Source File: bot.py    From xenon with GNU General Public License v3.0 6 votes vote down vote up
def block_check(loop):
    while True:
        try:
            time.sleep(1)
            future = asyncio.run_coroutine_threadsafe(asyncio.sleep(0), loop)
            blocked_for = 0
            while True:
                try:
                    future.result(1)
                    break
                except asyncio.TimeoutError:
                    blocked_for += 1
                    task = asyncio.current_task(loop)
                    buffer = io.StringIO()
                    task.print_stack(file=buffer)
                    buffer.seek(0)
                    log.warning("Event loop blocked for longer than %d seconds (%s)\n%s\n%s" % (
                        blocked_for,
                        str(task),
                        str(last_commands),
                        buffer.read()
                    ))
        except Exception:
            pass 
Example #9
Source File: UMRDriver.py    From UnifiedMessageRelay with MIT License 6 votes vote down vote up
def init_drivers():
    """
    bring up all the drivers
    this function should be called by UMRManager
    :return:
    """
    config = UMRConfig.config.Driver

    for driver_name, driver_config in config.items():
        if driver_config.Base not in driver_class_lookup_table:
            logger.error(f'Base driver "{driver_config.Base}" not found')
            exit(-1)
        driver: BaseDriverMixin = driver_class_lookup_table[driver_config.Base](driver_name)
        driver.start()
        driver_lookup_table[driver_name] = driver

    loop = asyncio.get_event_loop()

    for driver_name in config.keys():
        asyncio.run_coroutine_threadsafe(__post_init(driver_name), loop) 
Example #10
Source File: signal.py    From pai with Eclipse Public License 2.0 6 votes vote down vote up
def handle_message(self, timestamp, source, groupID, message, attachments):
        """ Handle Signal message. It should be a command """

        logger.debug("Received Message {} {} {} {} {}".format(
            timestamp, message, groupID, message, attachments))

        if source in cfg.SIGNAL_CONTACTS:
            future = asyncio.run_coroutine_threadsafe(self.handle_command(message), self.alarm.work_loop)
            ret = future.result(10)

            m = "Signal {} : {}".format(source, ret)
            logger.info(m)
        else:
            m = "Signal {} (UNK): {}".format(source, message)
            logger.warning(m)

        self.send_message(m, EventLevel.INFO)
        ps.sendNotification(Notification(sender=self.name, message=m, level=EventLevel.INFO)) 
Example #11
Source File: peer_outer_service.py    From loopchain with Apache License 2.0 6 votes vote down vote up
def __handler_get_tx_by_address(self, request, context):
        """Get Transaction by address

        :param request:
        :param context:
        :return:
        """
        params = json.loads(request.meta)
        address = params.pop('address', None)
        index = params.pop('index', None)

        if address is None or index is None:  # or params:
            return loopchain_pb2.Message(code=message_code.Response.fail_illegal_params)

        channel_stub = StubCollection().channel_stubs[request.channel]
        future = asyncio.run_coroutine_threadsafe(
            channel_stub.async_task().get_tx_by_address(address, index),
            self.peer_service.inner_service.loop
        )
        tx_list, next_index = future.result()
        tx_list_dumped = json.dumps(tx_list).encode(encoding=conf.PEER_DATA_ENCODING)

        return loopchain_pb2.Message(code=message_code.Response.success,
                                     meta=str(next_index),
                                     object=tx_list_dumped) 
Example #12
Source File: game_loop_process.py    From snakepit-game with The Unlicense 6 votes vote down vote up
def game_loop(asyncio_loop):
    # coroutine to run in main thread
    async def notify():
        await tick.acquire()
        tick.notify_all()
        tick.release()

    queue = Queue()

    # function to run in a different process
    def worker():
        while 1:
            print("doing heavy calculation in process {}".format(os.getpid()))
            sleep(1)
            queue.put("calculation result")

    Process(target=worker).start()

    while 1:
        # blocks this thread but not main thread with event loop
        result = queue.get()
        print("getting {} in process {}".format(result, os.getpid()))
        task = asyncio.run_coroutine_threadsafe(notify(), asyncio_loop)
        task.result() 
Example #13
Source File: peer_outer_service.py    From loopchain with Apache License 2.0 6 votes vote down vote up
def __get_status_cache(self, channel_name, time_in_seconds):
        """Cache status data.

        :param channel_name:
        :param time_in_seconds: An essential parameter for the `LRU cache` even if not used.

        :return:
        """
        try:
            channel_stub = StubCollection().channel_stubs[channel_name]
        except KeyError:
            raise ChannelStatusError(f"Invalid channel({channel_name})")

        if self.__status_cache is None:
            self.__status_cache = channel_stub.sync_task().get_status()
        else:
            future = asyncio.run_coroutine_threadsafe(
                channel_stub.async_task().get_status(),
                self.peer_service.inner_service.loop)
            future.add_done_callback(self.__set_status_cache)

        return self.__status_cache 
Example #14
Source File: peer_outer_service.py    From loopchain with Apache License 2.0 6 votes vote down vote up
def Stop(self, request, context):
        """Peer를 중지시킨다

        :param request: 중지요청
        :param context:
        :return: 중지결과
        """
        if request is not None:
            utils.logger.info('Peer will stop... by: ' + request.reason)

        try:
            for channel_name in conf.CHANNEL_OPTION:
                channel_stub = StubCollection().channel_stubs[channel_name]
                asyncio.run_coroutine_threadsafe(channel_stub.async_task().stop(), self.peer_service.inner_service.loop)

            self.peer_service.p2p_server_stop()

        except Exception as e:
            utils.logger.debug("Score Service Already stop by other reason. %s", e)

        return loopchain_pb2.StopReply(status="0") 
Example #15
Source File: test_tasks.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def target(self, fail=False, cancel=False, timeout=None,
               advance_coro=False):
        """Run add coroutine in the event loop."""
        coro = self.add(1, 2, fail=fail, cancel=cancel)
        future = asyncio.run_coroutine_threadsafe(coro, self.loop)
        if advance_coro:
            # this is for test_run_coroutine_threadsafe_task_factory_exception;
            # otherwise it spills errors and breaks **other** unittests, since
            # 'target' is interacting with threads.

            # With this call, `coro` will be advanced, so that
            # CoroWrapper.__del__ won't do anything when asyncio tests run
            # in debug mode.
            self.loop.call_soon_threadsafe(coro.send, None)
        try:
            return future.result(timeout)
        finally:
            future.done() or future.cancel() 
Example #16
Source File: asyncio_runner.py    From moler with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run_async_coroutine(self, coroutine_to_run, timeout):
        """Start coroutine in dedicated thread and await its result with timeout"""
        start_time = time.time()
        coro_future = self.start_async_coroutine(coroutine_to_run)
        # run_coroutine_threadsafe returns future as concurrent.futures.Future() and not asyncio.Future
        # so, we can await it with timeout inside current thread
        try:
            coro_result = coro_future.result(timeout=timeout)
            self.logger.debug("scheduled {} returned {}".format(coroutine_to_run, coro_result))
            return coro_result
        except concurrent.futures.TimeoutError:
            passed = time.time() - start_time
            raise MolerTimeout(timeout=timeout,
                               kind="run_async_coroutine({})".format(coroutine_to_run),
                               passed_time=passed)
        except concurrent.futures.CancelledError:
            raise 
Example #17
Source File: log.py    From maubot with GNU Affero General Public License v3.0 6 votes vote down vote up
def _emit(self, record: logging.LogRecord) -> None:
        # JSON conversion based on Marsel Mavletkulov's json-log-formatter (MIT license)
        # https://github.com/marselester/json-log-formatter
        content = {
            name: value
            for name, value in record.__dict__.items()
            if name not in EXCLUDE_ATTRS
        }
        content["id"] = str(record.relativeCreated)
        content["msg"] = record.getMessage()
        content["time"] = datetime.fromtimestamp(record.created)

        if record.exc_info:
            content["exc_info"] = self.formatter.formatException(record.exc_info)

        for name, value in content.items():
            if isinstance(value, datetime):
                content[name] = value.astimezone().isoformat()
        asyncio.run_coroutine_threadsafe(self.send(content), loop=self.loop)
        self.lines.append(content) 
Example #18
Source File: peer_outer_service.py    From loopchain with Apache License 2.0 6 votes vote down vote up
def CreateTx(self, request, context):
        """make tx by client request and broadcast it to the network

        :param request:
        :param context:
        :return:
        """
        channel_name = request.channel or conf.LOOPCHAIN_DEFAULT_CHANNEL
        utils.logger.info(f"peer_outer_service::CreateTx request({request.data}), channel({channel_name})")

        channel_stub = StubCollection().channel_stubs[channel_name]
        result_hash = asyncio.run_coroutine_threadsafe(
            channel_stub.async_task().create_tx(request.data),
            self.peer_service.inner_service.loop
        ).result()

        return loopchain_pb2.CreateTxReply(
            response_code=message_code.Response.success,
            tx_hash=result_hash,
            more_info='') 
Example #19
Source File: QAAsyncThread.py    From QUANTAXIS with MIT License 6 votes vote down vote up
def main(self):
        print('start')
        async_q = self._queue.async_q
        main_loop = asyncio.get_event_loop()
        while not (self._stopped and async_q.empty()):

            try:
                event = self.queue.get_nowait()
            except asyncio.QueueEmpty:
                pass
            else:
                asyncio.run_coroutine_threadsafe(
                    self.event_hadler(event),
                    main_loop
                )
                async_q.task_done()
            await asyncio.sleep(0.0001) 
Example #20
Source File: utils.py    From aiocqhttp with MIT License 5 votes vote down vote up
def sync_wait(coro: Awaitable[Any],
              loop: asyncio.AbstractEventLoop) -> Any:
    """
    在 `loop` 中线程安全地运行 `coro`,并同步地等待其运行完成,返回运行结果。
    """
    fut = asyncio.run_coroutine_threadsafe(coro, loop)
    return fut.result() 
Example #21
Source File: schwifty.py    From mee6 with MIT License 5 votes vote down vote up
def run(self):
        while not self._stop_ev.wait(self.interval):
            coro = self.ws.send(self.ws.OPCODE_HEARTBEAT, d=1337)
            f = asyncio.run_coroutine_threadsafe(coro, loop=self.ws.loop)
            try:
                f.result()
                self.ws.heartbeat_ack = False
            except Exception:
                self.stop() 
Example #22
Source File: node_1.py    From protoactor-python with Apache License 2.0 5 votes vote down vote up
def receive(self, context: AbstractContext) -> None:
        message = context.message
        if isinstance(message, Pong):
            self._count += 1
            if self._count % 5 == 0:
                print(self._count)
            if self._count == self._message_count:
                asyncio.run_coroutine_threadsafe(self.notify(), self._loop) 
Example #23
Source File: loop.py    From switchio with Mozilla Public License 2.0 5 votes vote down vote up
def start(self):
        '''Start this loop's listen coroutine and start processing
        all received events.
        '''
        self.log.debug("Starting event loop server")
        if not self._con.connected():
            raise utils.ConfigurationError("you must call 'connect' first")

        self._entry_fut = asyncio.run_coroutine_threadsafe(
            self._listen_forever(), loop=self.loop) 
Example #24
Source File: loop.py    From switchio with Mozilla Public License 2.0 5 votes vote down vote up
def connect(self, loop=None, timeout=3, debug=False, **conn_kwargs):
        '''Initialize underlying receive connection.
        '''
        # TODO: once we remove SWIG/py27 support this check can be removed
        if self.connected() and self.is_alive():
            raise utils.ConfigurationError(
                "event loop is already active, call 'disconnect()' first")
        elif self.connected():
                self.log.info("event loop is already connected")
                return

        if not self.is_alive():
            self._launch_bg_loop(debug=debug)
            while not self.loop:
                time.sleep(0.1)

        future = asyncio.run_coroutine_threadsafe(
            self._con.connect(block=False, loop=self.loop, **conn_kwargs),
            self.loop
        )
        future.result(3)  # pass through any timeout or conn errors

        # subscribe for events
        self._con.subscribe(
            (ev for ev in self._handlers if ev not in self._unsub))
        self.log.info("Connected event loop '{}' to '{}'".format(self._id,
                      self.host)) 
Example #25
Source File: __init__.py    From nonebot with MIT License 5 votes vote down vote up
def __init__(self,
                 bot: NoneBot,
                 event: CQEvent,
                 cmd: Command,
                 *,
                 current_arg: Optional[str] = '',
                 args: Optional[CommandArgs_T] = None):
        super().__init__(bot, event)
        self.cmd = cmd  # Command object

        # unique key of the argument that is currently requesting (asking)
        self.current_key: Optional[str] = None

        # initialize current argument filters
        self.current_arg_filters: Optional[List[Filter_T]] = None

        self._current_send_kwargs: Dict[str, Any] = {}

        # initialize current argument
        self.current_arg: Optional[str] = ''  # with potential CQ codes
        self._current_arg_text = None
        self._current_arg_images = None
        self.refresh(event, current_arg=current_arg)  # fill the above

        self._run_future = partial(asyncio.run_coroutine_threadsafe,
                                   loop=bot.loop)

        self._state: State_T = {}
        if args:
            self._state.update(args)

        self._last_interaction = None  # last interaction time of this session
        self._running = False 
Example #26
Source File: __init__.py    From target-stitch with GNU Affero General Public License v3.0 5 votes vote down vote up
def handle_state_only(self, state_writer=None, state=None):
        async def fake_future_fn():
            pass

        global PENDING_REQUESTS
        #NB> no point in sending out this state if a previous request has failed
        check_send_exception()
        future = asyncio.run_coroutine_threadsafe(fake_future_fn(), new_loop)
        next_pending_request = (future, state)
        PENDING_REQUESTS.append(next_pending_request)

        future.add_done_callback(functools.partial(self.flush_states, state_writer)) 
Example #27
Source File: __init__.py    From target-stitch with GNU Affero General Public License v3.0 5 votes vote down vote up
def send(self, data, contains_activate_version, state_writer, state, stitch_url):
        '''Send the given data to Stitch, retrying on exceptions'''
        global PENDING_REQUESTS
        global SEND_EXCEPTION

        check_send_exception()

        headers = self.headers()
        verify_ssl = True
        if os.environ.get("TARGET_STITCH_SSL_VERIFY") == 'false':
            verify_ssl = False

        LOGGER.info("Sending batch of %d bytes to %s", len(data), stitch_url)

        #NB> before we send any activate_versions we must ensure that all PENDING_REQUETS complete.
        #this is to ensure ordering in the case of Full Table replication where the Activate Version,
        #must arrive AFTER all of the relevant data.
        if len(PENDING_REQUESTS) > 0 and contains_activate_version:
            LOGGER.info('Sending batch with ActivateVersion. Flushing PENDING_REQUESTS first')
            finish_requests()

        if len(PENDING_REQUESTS) >= CONFIG.get('turbo_boost_factor'):

            #wait for to finish the first future before resuming the main thread
            finish_requests(CONFIG.get('turbo_boost_factor') - 1)

        #NB> this schedules the task on the event loop thread.
        #    it will be executed at some point in the future
        future = asyncio.run_coroutine_threadsafe(post_coroutine(stitch_url, headers, data, verify_ssl), new_loop)
        next_pending_request = (future, state)
        PENDING_REQUESTS.append(next_pending_request)
        future.add_done_callback(functools.partial(self.flush_states, state_writer)) 
Example #28
Source File: coroutine_threadsafe.py    From nats.py with Apache License 2.0 5 votes vote down vote up
def request(self, subject, data):
        # Required to be able to run the coroutine in the proper thread.
        future = asyncio.run_coroutine_threadsafe(
            Component.component.request(subject, data),
            loop=self.loop)
        return future.result() 
Example #29
Source File: coroutine_threadsafe.py    From nats.py with Apache License 2.0 5 votes vote down vote up
def publish(self, subject, data):
        # Required to be able to run the coroutine in the proper thread.
        asyncio.run_coroutine_threadsafe(
            Component.component.publish(subject,data),
            loop=self.loop) 
Example #30
Source File: thread_delegating_executor.py    From federated with Apache License 2.0 5 votes vote down vote up
def _delegate_with_trace_ctx(coro, event_loop):
  coro_with_trace_ctx = tracing.wrap_coroutine_in_current_trace_context(coro)
  return asyncio.wrap_future(
      asyncio.run_coroutine_threadsafe(coro_with_trace_ctx, event_loop))