Python asyncio 模块,async() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用asyncio.async()

项目:kas    作者:siemens    | 项目源码 | 文件源码
def repos_fetch(config, repos):
    """
        Fetches the list of repositories to the kas_work_dir.
    """
    tasks = []
    for repo in repos:
        if not hasattr(asyncio, 'ensure_future'):
            # pylint: disable=no-member,deprecated-method
            task = asyncio.async(_repo_fetch_async(config, repo))
        else:
            task = asyncio.ensure_future(_repo_fetch_async(config, repo))
        tasks.append(task)

    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.wait(tasks))

    for task in tasks:
        if task.result():
            sys.exit(task.result())
项目:python-insteonplm    作者:nugget    | 项目源码 | 文件源码
def monitor():
    """Wrapper to call console with a loop."""
    devicelist = (
        {
            "address": "3c4fc5",
            "cat": 0x05,
            "subcat": 0x0b,
            "firmware": 0x00
        },
        {
            "address": "43af9b",
            "cat": 0x02,
            "subcat": 0x1a,
            "firmware": 0x00
        }
    )
    log = logging.getLogger(__name__)
    loop = asyncio.get_event_loop()
    asyncio.async(console(loop, log, devicelist))
    loop.run_forever()
项目:rci    作者:seecloud    | 项目源码 | 文件源码
def start(self):
        self.hosts = [Host(c, self, self.root)
                      for c in self.config["hosts"]]
        mds_cfg = self.config.get("metadata_server", {})
        mds_addr = mds_cfg.get("listen_addr", "0.0.0.0")
        mds_port = mds_cfg.get("listen_port", 8088)
        self.mds = clis.Server(self.root.loop,
                               listen_addr=mds_addr,
                               listen_port=mds_port,
                               ssh_keys=self.root.config.get_ssh_keys())
        self.mds_future = asyncio.async(self.mds.run(), loop=self.root.loop)
        command = ("PREROUTING -d 169.254.169.254 -p tcp --dport 80 "
                   "-j DNAT --to-destination %s:%s")
        for host in self.hosts:
            if mds_addr == "0.0.0.0":
                my_addr = utils.get_local_address(host.ssh.hostname)
            else:
                my_addr = mds_addr
            cmd = command % (my_addr, mds_port)
            yield from host.ssh.run(("iptables -t nat -C %s ||"
                                     "iptables -t nat -I %s") % (cmd, cmd))
项目:annotated-py-asyncio    作者:hhstore    | 项目源码 | 文件源码
def test_create_task(self):
        class MyTask(asyncio.Task):
            pass

        @asyncio.coroutine
        def test():
            pass

        class EventLoop(base_events.BaseEventLoop):
            def create_task(self, coro):
                return MyTask(coro, loop=loop)

        loop = EventLoop()
        self.set_event_loop(loop)

        coro = test()
        task = asyncio.async(coro, loop=loop)
        self.assertIsInstance(task, MyTask)

        # make warnings quiet
        task._log_destroy_pending = False
        coro.close()
项目:annotated-py-asyncio    作者:hhstore    | 项目源码 | 文件源码
def test_async_coroutine(self):
        @asyncio.coroutine
        def notmuch():
            return 'ok'
        t = asyncio.async(notmuch(), loop=self.loop)
        self.loop.run_until_complete(t)
        self.assertTrue(t.done())
        self.assertEqual(t.result(), 'ok')
        self.assertIs(t._loop, self.loop)

        loop = asyncio.new_event_loop()
        self.set_event_loop(loop)
        t = asyncio.async(notmuch(), loop=loop)
        self.assertIs(t._loop, loop)
        loop.run_until_complete(t)
        loop.close()
项目:annotated-py-asyncio    作者:hhstore    | 项目源码 | 文件源码
def test_async_task(self):
        @asyncio.coroutine
        def notmuch():
            return 'ok'
        t_orig = asyncio.Task(notmuch(), loop=self.loop)
        t = asyncio.async(t_orig)
        self.loop.run_until_complete(t)
        self.assertTrue(t.done())
        self.assertEqual(t.result(), 'ok')
        self.assertIs(t, t_orig)

        loop = asyncio.new_event_loop()
        self.set_event_loop(loop)

        with self.assertRaises(ValueError):
            t = asyncio.async(t_orig, loop=loop)

        loop.close()

        t = asyncio.async(t_orig, loop=self.loop)
        self.assertIs(t, t_orig)
项目:annotated-py-asyncio    作者:hhstore    | 项目源码 | 文件源码
def test_shield_effect(self):
        # Cancelling outer() does not affect inner().
        proof = 0
        waiter = asyncio.Future(loop=self.loop)

        @asyncio.coroutine
        def inner():
            nonlocal proof
            yield from waiter
            proof += 1

        @asyncio.coroutine
        def outer():
            nonlocal proof
            yield from asyncio.shield(inner(), loop=self.loop)
            proof += 100

        f = asyncio.async(outer(), loop=self.loop)
        test_utils.run_briefly(self.loop)
        f.cancel()
        with self.assertRaises(asyncio.CancelledError):
            self.loop.run_until_complete(f)
        waiter.set_result(None)
        test_utils.run_briefly(self.loop)
        self.assertEqual(proof, 1)
项目:annotated-py-asyncio    作者:hhstore    | 项目源码 | 文件源码
def test_exception_marking(self):
        # Test for the first line marked "Mark exception retrieved."

        @asyncio.coroutine
        def inner(f):
            yield from f
            raise RuntimeError('should not be ignored')

        a = asyncio.Future(loop=self.one_loop)
        b = asyncio.Future(loop=self.one_loop)

        @asyncio.coroutine
        def outer():
            yield from asyncio.gather(inner(a), inner(b), loop=self.one_loop)

        f = asyncio.async(outer(), loop=self.one_loop)
        test_utils.run_briefly(self.one_loop)
        a.set_result(None)
        test_utils.run_briefly(self.one_loop)
        b.set_result(None)
        test_utils.run_briefly(self.one_loop)
        self.assertIsInstance(f.exception(), RuntimeError)
项目:guillotina    作者:plone    | 项目源码 | 文件源码
def setup(self, app):
        '''
        need to manually run this after app is initialized and we have
        locals that matter to us...
        '''
        app_settings['root_user']['password'] = TESTING_SETTINGS['root_user']['password']
        root = get_utility(IApplication, name='root')
        helpers = ShellHelpers(app, root, self.request)
        _locals = {
            'app': app,
            'root': root,
            'app_settings': app_settings,
            'request': self.request,
            'helpers': helpers,
            'use_db': helpers.use_db,
            'use_container': helpers.use_container,
            'commit': helpers.commit,
            'abort': helpers.abort
        }
        self.console = self.console_class(None, locals=_locals, loop=self)
        coro = self.console.interact(self.banner, stop=True, handle_sigint=True)
        self.console_task = asyncio.async(coro, loop=self)
项目:hangoutsbot    作者:das7pad    | 项目源码 | 文件源码
def aiohttp_start(bot, name, port, certfile, requesthandlerclass, group,
                  callback=None):
    requesthandler = requesthandlerclass(bot)

    app = web.Application()
    requesthandler.addroutes(app.router)

    handler = app.make_handler()

    if certfile:
        sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        sslcontext.load_cert_chain(certfile)
    else:
        sslcontext = None

    loop = asyncio.get_event_loop()
    server = loop.create_server(handler, name, port, ssl=sslcontext)

    asyncio.async(server).add_done_callback(
        functools.partial(aiohttp_started, handler=handler, app=app,
                          group=group, callback=callback))

    tracking.register_aiohttp_web(group)
项目:spqrel_tools    作者:LCAS    | 项目源码 | 文件源码
def talker(self, port=8128):
        factory = WebSocketServerFactory(u"ws://0.0.0.0:%d" % port)
        factory.protocol = self.protocol

        self.loop = asyncio.get_event_loop()
        coro = self.loop.create_server(factory, '0.0.0.0', port)
        server = self.loop.run_until_complete(coro)
        asyncio.async(self.wait_until_shutdown(self.loop))

        #signal.signal(signal.SIGINT, self.signal_handler)
        self.loop.run_forever()

        info("Closing...")
        server.close()

        self.loop.run_until_complete(server.wait_closed())
        self.loop.close()
项目:arduino-ciao-meteor-ddp-connector    作者:andrea689    | 项目源码 | 文件源码
def connection_made(self, transport):
        """
        A peer is now connected and we receive an instance
        of the underlying :class:`asyncio.Transport`.

        We :class:`asyncio.StreamReader` is created
        and the transport is associated before the
        initial HTTP handshake is undertaken.
        """
        #self.transport = transport
        #self.stream = asyncio.StreamReader()
        #self.stream.set_transport(transport)
        asyncio.StreamReaderProtocol.connection_made(self, transport)
        # Let make it concurrent for others to tag along
        f = asyncio.async(self.handle_initial_handshake())
        f.add_done_callback(self.terminated)
项目:wptagent    作者:WPO-Foundation    | 项目源码 | 文件源码
def connection_made(self, transport):
        """
        A peer is now connected and we receive an instance
        of the underlying :class:`asyncio.Transport`.

        We :class:`asyncio.StreamReader` is created
        and the transport is associated before the
        initial HTTP handshake is undertaken.
        """
        #self.transport = transport
        #self.stream = asyncio.StreamReader()
        #self.stream.set_transport(transport)
        asyncio.StreamReaderProtocol.connection_made(self, transport)
        # Let make it concurrent for others to tag along
        f = asyncio.async(self.handle_initial_handshake())
        f.add_done_callback(self.terminated)
项目:PythonTrading    作者:F2011B    | 项目源码 | 文件源码
def data_received(self, data):

        print('data received: ', data.decode())
        message = data.decode()
        print(message)
        add_list = list()
        for element in message.split('_End') :
            if 'Add' in element:
                add_list.append(element.split('_')[1])
                print(add_list)


            if len(add_list) > 0:
                add_event.data = add_list
                add_event.set()

            if 'Send' in element:
                print('Send was sent')
                send_event.set()
                fut = asyncio.async(q.get())
                fut.add_done_callback(self.write_reply)
项目:PythonTrading    作者:F2011B    | 项目源码 | 文件源码
def data_received(self, data):

        print('data received: ', data.decode())
        message = data.decode()
        print(message)
        add_list = list()
        for element in message.split('_End') :
            if 'Add' in element:
                add_list.append(element.split('_')[1])
                print(add_list)


            if len(add_list) > 0:
                add_event.data = add_list
                add_event.set()

            if 'Send' in element:
                print('Send was sent')
                send_event.set()
                fut = asyncio.async(q.get())
                fut.add_done_callback(self.write_reply)
项目:PythonTrading    作者:F2011B    | 项目源码 | 文件源码
def data_received(self, data):

        print('data received: ', data.decode())
        message = data.decode()
        print(message)
        add_list = list()
        for element in message.split('_End') :
            if 'Add' in element:
                add_list.append(element.split('_')[1])
                print(add_list)


            if len(add_list) > 0:
                add_event.data = add_list
                add_event.set()

            if 'Send' in element:
                print('Send was sent')
                send_event.set()
                fut = asyncio.async(q.get())
                fut.add_done_callback(self.write_reply)
项目:dcos    作者:dcos    | 项目源码 | 文件源码
def run_commands_chain_async(self, chains: list, block=False, state_json_dir=None, delegate_extra_params={}):
        sem = asyncio.Semaphore(self.__parallelism)

        if state_json_dir:
            log.debug('Using default JsonDelegate method, state_json_dir {}'.format(state_json_dir))
            self.async_delegate = JsonDelegate(state_json_dir, len(self.__targets), **delegate_extra_params)
        else:
            assert self.async_delegate, 'async delegate must be set'

        if block:
            log.debug('Waiting for run_command_chain_async to execute')
            tasks = []
            for host in self.__targets:
                tasks.append(asyncio.async(self.dispatch_chain(host, chains, sem)))

            yield from asyncio.wait(tasks)
            log.debug('run_command_chain_async executed')
            return [task.result() for task in tasks]
        else:
            log.debug('Started run_command_chain_async in non-blocking mode')
            for host in self.__targets:
                asyncio.async(self.dispatch_chain(host, chains, sem))
项目:morphis    作者:bitcoinembassy    | 项目源码 | 文件源码
def do_GET(self):
        self._prepare_for_request()

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Handler do_GET(): path=[{}].".format(self.path))
            req_dict.append(self)

        self.loop.call_soon_threadsafe(\
            asyncio.async,\
            self._dispatcher.do_GET(self._get_rpath()))

        self._write_response()

        if self.node.web_devel and self.headers["Cache-Control"] == "no-cache":
            global _concurrent_request_count
            with _request_lock:
                _concurrent_request_count -= 1

        if log.isEnabledFor(logging.DEBUG):
            req_dict.remove(self)
            log.debug("Done do_GET(): path=[{}], reqs=[{}]."\
                .format(self.path, len(req_dict)))
项目:morphis    作者:bitcoinembassy    | 项目源码 | 文件源码
def do_POST(self):
        self._prepare_for_request()

        self.loop.call_soon_threadsafe(\
            asyncio.async,\
            self._dispatcher.do_POST(self._get_rpath()))

        log.debug("Reading request.")
        self._read_request()

        log.debug("Writing response.")
        self._write_response()

        if self.node.web_devel and self.headers["Cache-Control"] == "no-cache":
            global _concurrent_request_count
            with _request_lock:
                _concurrent_request_count -= 1

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Done do_POST(): path=[{}].".format(self.path))
项目:morphis    作者:bitcoinembassy    | 项目源码 | 文件源码
def _read_request(self):
        inq = self._inq
        loop = self.loop

        rlen = int(self.headers["Content-Length"])

        while rlen:
            data = self.rfile.read(min(rlen, 65536))
            self.loop.call_soon_threadsafe(\
                functools.partial(\
                    asyncio.async,\
                    inq.put(data),\
                    loop=self.loop))
            rlen -= len(data)

        self.loop.call_soon_threadsafe(\
            functools.partial(\
                asyncio.async,\
                inq.put(None),\
                loop=self.loop))
项目:morphis    作者:bitcoinembassy    | 项目源码 | 文件源码
def update_scan_interval(self, interval):
        if not interval:
            self._running = False
            if self._task:
                self._task.cancel()
            return

        self.scan_interval = interval

        if self._running:
            if log.isEnabledFor(logging.INFO):
                log.info("Notifying DmailAutoscanProcess (addr=[{}]) of"\
                    " interval change."\
                        .format(mbase32.encode(self.dmail_address.site_key)))
            if self._task:
                self._task.cancel()
        else:
            if log.isEnabledFor(logging.INFO):
                log.info("Starting DmailAutoscanProcess (addr=[{}])."\
                    .format(mbase32.encode(self.dmail_address.site_key)))
            asyncio.async(self.run(), loop=self.loop)
项目:pomoxis    作者:nanoporetech    | 项目源码 | 文件源码
def dispatch(self, event):
        """Dispatch an event after filtering. We handle
        creation and move events only.

        :param event: watchdog event.

        :returns: None
        """
        if event.event_type not in (EVENT_TYPE_CREATED, EVENT_TYPE_MOVED):
            return
        if self.ignore_directories and event.is_directory:
            return

        paths = []
        if has_attribute(event, 'dest_path'):
            paths.append(unicode_paths.decode(event.dest_path))
        if event.src_path:
            paths.append(unicode_paths.decode(event.src_path))

        if any(r.match(p) for r in self.ignore_regexes for p in paths):
            return

        if any(r.match(p) for r in self.regexes for p in paths):
            self._loop.call_soon_threadsafe(asyncio.async, self._process_file(event))
项目:pyatv    作者:postlund    | 项目源码 | 文件源码
def start(self, initial_delay=0):
        """Wait for push updates from device.

        Will throw NoAsyncListenerError if no listner has been set.
        """
        if self._future is not None:
            raise exceptions.NoAsyncListenerError

        # If ensure_future, use that instead of async
        if hasattr(asyncio, 'ensure_future'):
            run_async = getattr(asyncio, 'ensure_future')
        else:
            run_async = asyncio.async  # pylint: disable=no-member

        # Always start with 0 to trigger an immediate response for the
        # first request
        self._atv.playstatus_revision = 0

        # This for some reason fails on travis but not in other places.
        # Why is that (same python version)?
        # pylint: disable=deprecated-method
        self._future = run_async(self._poller(initial_delay),
                                 loop=self._loop)
        return self._future
项目:oecluster    作者:OECFHTW    | 项目源码 | 文件源码
def connect_to_nodes(self):
        while True:
            for ip, host in self._host_list.items():
                if ip == ipaddress.ip_address('192.168.0.246'):
                    xyz = 0

                if ip not in self._member_list or self._member_list[ip].server_connection is None:
                    #if not hasattr(self._host_list[ip].server_connection, 'test'):
                    if not (
                            type(self._host_list[ip].server_connection) is AsyncClient
                            or hasattr(self._host_list[ip].server_connection, 'peer_name')
                    ):
                        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                        sock.settimeout(0.05)
                        port = self._port
                        if sock.connect_ex((str(ip), port)) == 0:
                            self._host_list[ip].server_connection = client = AsyncClient.AsyncClient(str(ip))
                            yield from asyncio.async(client.connect())
                        else:
                            logger.debug('Cluster not running on {}'.format(self._host_list[ip]))
            yield
项目:epycyzm    作者:slush0    | 项目源码 | 文件源码
def run(self):
        print("Starting CPU solver")
        s = Solver()

        while self.job == None or self.nonce1 == None:
            time.sleep(2)
            print(".", end='', flush=True)

        while not self._stop:
            nonce2 = self.increase_nonce()
            nonce2 = nonce2.rjust(32 - len(self.nonce1) - len(self.solver_nonce), b'\0')

            header = self.job.build_header(self.nonce1 + self.solver_nonce + nonce2)

            sol_cnt = s.find_solutions(header)
            self.counter(sol_cnt) # Increase counter for stats

            for i in range(sol_cnt):
                solution = b'\xfd\x40\x05' + s.get_solution(i)

                if self.job.is_valid(header, solution, self.job.target):
                    print("FOUND VALID SOLUTION!")
                    # asyncio.run_coroutine_threadsafe(self.on_share(self.job, self.solver_nonce + nonce2, solution), self.loop)
                    asyncio.async(self.on_share(self.job, self.solver_nonce + nonce2, solution), loop=self.loop)
项目:circular    作者:jonathanverner    | 项目源码 | 文件源码
def __setattr__(self, attr, val):
        if attr.startswith('_'):
            super().__setattr__(attr, val)
        else:
            if isinstance(val, list):
                self._dct[attr] = ListProxy(val)
            elif isinstance(val, dict):
                self._dct[attr] = DictProxy(val)
            elif asyncio.iscoroutine(val) or asyncio.iscoroutinefunction(val) or isinstance(val, asyncio.Future):
                val = asyncio.async(val)

                def set_later(future_val, attr=attr):
                    setattr(self, attr, future_val.result())

                val.add_done_callback(set_later)
            else:
                self._dct[attr] = val
项目:circular    作者:jonathanverner    | 项目源码 | 文件源码
def __setattr__(self, attr, val):
        if attr.startswith('_'):
            super().__setattr__(attr, val)
        else:
            if isinstance(val, list):
                self._dct[attr] = ListProxy(val)
            elif isinstance(val, dict):
                self._dct[attr] = DictProxy(val)
            elif asyncio.iscoroutine(val) or asyncio.iscoroutinefunction(val) or isinstance(val, asyncio.Future):
                val = asyncio.async(val)

                def set_later(future_val, attr=attr):
                    setattr(self, attr, future_val.result())

                val.add_done_callback(set_later)
            else:
                self._dct[attr] = val
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_create_task(self):
        class MyTask(asyncio.Task):
            pass

        @asyncio.coroutine
        def test():
            pass

        class EventLoop(base_events.BaseEventLoop):
            def create_task(self, coro):
                return MyTask(coro, loop=loop)

        loop = EventLoop()
        self.set_event_loop(loop)

        coro = test()
        task = asyncio.async(coro, loop=loop)
        self.assertIsInstance(task, MyTask)

        # make warnings quiet
        task._log_destroy_pending = False
        coro.close()
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_async_coroutine(self):
        @asyncio.coroutine
        def notmuch():
            return 'ok'
        t = asyncio.async(notmuch(), loop=self.loop)
        self.loop.run_until_complete(t)
        self.assertTrue(t.done())
        self.assertEqual(t.result(), 'ok')
        self.assertIs(t._loop, self.loop)

        loop = asyncio.new_event_loop()
        self.set_event_loop(loop)
        t = asyncio.async(notmuch(), loop=loop)
        self.assertIs(t._loop, loop)
        loop.run_until_complete(t)
        loop.close()
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_async_task(self):
        @asyncio.coroutine
        def notmuch():
            return 'ok'
        t_orig = asyncio.Task(notmuch(), loop=self.loop)
        t = asyncio.async(t_orig)
        self.loop.run_until_complete(t)
        self.assertTrue(t.done())
        self.assertEqual(t.result(), 'ok')
        self.assertIs(t, t_orig)

        loop = asyncio.new_event_loop()
        self.set_event_loop(loop)

        with self.assertRaises(ValueError):
            t = asyncio.async(t_orig, loop=loop)

        loop.close()

        t = asyncio.async(t_orig, loop=self.loop)
        self.assertIs(t, t_orig)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_shield_effect(self):
        # Cancelling outer() does not affect inner().
        proof = 0
        waiter = asyncio.Future(loop=self.loop)

        @asyncio.coroutine
        def inner():
            nonlocal proof
            yield from waiter
            proof += 1

        @asyncio.coroutine
        def outer():
            nonlocal proof
            yield from asyncio.shield(inner(), loop=self.loop)
            proof += 100

        f = asyncio.async(outer(), loop=self.loop)
        test_utils.run_briefly(self.loop)
        f.cancel()
        with self.assertRaises(asyncio.CancelledError):
            self.loop.run_until_complete(f)
        waiter.set_result(None)
        test_utils.run_briefly(self.loop)
        self.assertEqual(proof, 1)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_exception_marking(self):
        # Test for the first line marked "Mark exception retrieved."

        @asyncio.coroutine
        def inner(f):
            yield from f
            raise RuntimeError('should not be ignored')

        a = asyncio.Future(loop=self.one_loop)
        b = asyncio.Future(loop=self.one_loop)

        @asyncio.coroutine
        def outer():
            yield from asyncio.gather(inner(a), inner(b), loop=self.one_loop)

        f = asyncio.async(outer(), loop=self.one_loop)
        test_utils.run_briefly(self.one_loop)
        a.set_result(None)
        test_utils.run_briefly(self.one_loop)
        b.set_result(None)
        test_utils.run_briefly(self.one_loop)
        self.assertIsInstance(f.exception(), RuntimeError)
项目:aioimaplib    作者:bamthomas    | 项目源码 | 文件源码
def critical_section(next_state):
    @asyncio.coroutine
    def execute_section(self, state, critical_func, *args, **kwargs):
        with (yield from self.state_condition):
            critical_func(self, *args, **kwargs)
            self.state = state
            log.debug('state -> %s' % state)
            self.state_condition.notify_all()

    def decorator(func):
        def wrapper(self, *args, **kwargs):
            asyncio.async(execute_section(self, next_state, func, *args, **kwargs))

        return update_wrapper(wrapper, func)

    return decorator
项目:aioimaplib    作者:bamthomas    | 项目源码 | 文件源码
def wait_for_new_message(host, user, password):
    imap_client = aioimaplib.IMAP4_SSL(host=host)
    yield from imap_client.wait_hello_from_server()

    yield from imap_client.login(user, password)
    yield from imap_client.select()

    asyncio.async(imap_client.idle())
    while True:
        msg = yield from imap_client.wait_server_push()
        print('--> received from server: %s' % msg)
        if 'EXISTS' in msg:
            imap_client.idle_done()
            break

    yield from imap_client.logout()
项目:aioimaplib    作者:bamthomas    | 项目源码 | 文件源码
def _handle_line(self, line, current_cmd):
        if not line:
            return

        if self.state == CONNECTED:
            asyncio.async(self.welcome(line))
        elif tagged_status_response_re.match(line):
            self._response_done(line)
        elif current_cmd is not None:
            current_cmd.append_to_resp(line)
            return current_cmd
        elif line.startswith('*'):
            return self._untagged_response(line)
        elif line.startswith('+'):
            self._continuation(line)
        else:
            log.info('unknown data received %s' % line)
项目:trellio    作者:artificilabs    | 项目源码 | 文件源码
def _publish(self, endpoint, payload):
        channel = self._get_pubsub_channel(endpoint)
        asyncio.async(self._pubsub_handler.publish(channel, json.dumps(payload, cls=TrellioEncoder)))
项目:trellio    作者:artificilabs    | 项目源码 | 文件源码
def subscription_handler(self, channel, payload):
        service, version, endpoint = channel.split('/')
        func = getattr(self, endpoint)
        asyncio.async(func(**json.loads(payload)))
项目:trellio    作者:artificilabs    | 项目源码 | 文件源码
def _get_subscribe_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        coroutine_func = func
        if not iscoroutine(func):
            coroutine_func = coroutine(func)
        return (async(coroutine_func(*args, **kwargs)))

    return wrapper
项目:trellio    作者:artificilabs    | 项目源码 | 文件源码
def _enable_http_middleware(func):  # pre and post http, processing
    @wraps(func)
    async def f(self, *args, **kwargs):
        if hasattr(self, 'middlewares'):
            for i in self.middlewares:
                if hasattr(i, 'pre_request'):
                    pre_request = getattr(i, 'pre_request')
                    if callable(pre_request):
                        try:
                            res = await pre_request(self, *args, **kwargs)  # passing service as first argument
                            if res:
                                return res
                        except Exception as e:
                            return Response(status=400, content_type='application/json',
                                            body=json.dumps(
                                                {'error': str(e), 'sector': getattr(i, 'middleware_info')}).encode())
        _func = coroutine(func)  # func is a generator object
        result = await _func(self, *args, **kwargs)
        if hasattr(self, 'middlewares'):
            for i in self.middlewares:
                if hasattr(i, 'post_request'):
                    post_request = getattr(i, 'post_request')
                    if callable(post_request):
                        try:
                            res = await post_request(self, result, *args, **kwargs)
                            if res:
                                return res
                        except Exception as e:
                            return Response(status=400, content_type='application/json',
                                            body=json.dumps(
                                                {'error': str(e), 'sector': getattr(i, 'middleware_info')}).encode())

        return result

    return f
项目:trellio    作者:artificilabs    | 项目源码 | 文件源码
def initiate(self):
        self.tcp_bus.register()
        # yield from self.pubsub_bus.create_pubsub_handler()
        # async(self.pubsub_bus.register_for_subscription(self.host, self.port, self.node_id, self.clients))
项目:trellio    作者:artificilabs    | 项目源码 | 文件源码
def pong_received(self, payload=None):
        """
        Called when a pong is received. So the timer is cancelled
        """
        if self._timer is not None:
            self._timer.cancel()
            self._failures = 0
            asyncio.async(self.send_ping(payload=payload))
项目:zanph    作者:zanph    | 项目源码 | 文件源码
def start(self):
        """Start ZAP authentication"""
        super().start()
        self.__poller = Poller()
        self.__poller.register(self.zap_socket, zmq.POLLIN)
        self.__task = asyncio.async(self.__handle_zap())
项目:rci    作者:seecloud    | 项目源码 | 文件源码
def start_coro(self, coro):
        fut = asyncio.async(self.run_coro(coro), loop=self.loop)
        self._running_coros.add(fut)
        fut.add_done_callback(self._running_coros.remove)
        return fut
项目:rci    作者:seecloud    | 项目源码 | 文件源码
def start_obj(self, obj):
        fut = asyncio.async(self.run_obj(obj), loop=self.loop)
        self._running_objects[fut] = obj
        if hasattr(obj, "cleanup"):
            fut.add_done_callback(self.schedule_cleanup)
        else:
            fut.add_done_callback(self._running_objects.pop)
        return fut
项目:rci    作者:seecloud    | 项目源码 | 文件源码
def schedule_cleanup(self, fut):
        obj = self._running_objects.pop(fut)
        fut = asyncio.async(self.run_cleanup(obj), loop=self.loop)
        self._running_cleanups.add(fut)
        fut.add_done_callback(self._running_cleanups.remove)
项目:rci    作者:seecloud    | 项目源码 | 文件源码
def run(self):
        self._load_config()

        for service in self.config.iter_instances("service", "Service"):
            self._start_service(service)

        listen = self.config.raw_data[0]["core"]["listen"]

        self.http = HTTP(self.loop, listen)
        self.http.add_route("*", r"/{dir:.*}", self._http_handler)

        for prov in self.config.iter_providers():
            self.providers[prov.name] = prov
            yield from prov.start()
        yield from self.http.start()
        reload_fut = asyncio.async(self.reload(), loop=self.loop)
        yield from self.stop_event.wait()
        self.log.info("Interrupted.")
        reload_fut.cancel()
        yield from reload_fut
        for obj in self._running_objects:
            obj.cancel()
        yield from asyncio.wait(self._running_objects,
                                return_when=futures.ALL_COMPLETED)
        if self._running_cleanups:
            yield from asyncio.wait(self._running_cleanups,
                                    return_when=futures.ALL_COMPLETED)
        for provider in self.providers.values():
            yield from prov.stop()
        self.http.stop()
        yield from self.http.wait_closed()
        self.log.info("Exit.")
项目:PresentationDriver    作者:pasalino    | 项目源码 | 文件源码
def startRemote():
    global serialPort
    serialPort = serial.Serial(port=args.port, baudrate=9600, timeout=.1)
    signal.signal(signal.SIGINT, close_listener)
    print("Start listen remote command. Open presentation and use remote")

    loop = asyncio.get_event_loop()
    try:
        asyncio.async(start_listen())
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        loop.close()
        close_listener()
项目:annotated-py-asyncio    作者:hhstore    | 项目源码 | 文件源码
def test_close(self):
        a, b = self.loop._socketpair()
        trans = self.loop._make_socket_transport(a, asyncio.Protocol())
        f = asyncio.async(self.loop.sock_recv(b, 100))
        trans.close()
        self.loop.run_until_complete(f)
        self.assertEqual(f.result(), b'')
        b.close()
项目:annotated-py-asyncio    作者:hhstore    | 项目源码 | 文件源码
def test_default_exc_handler_coro(self):
        self.loop._process_events = mock.Mock()

        @asyncio.coroutine
        def zero_error_coro():
            yield from asyncio.sleep(0.01, loop=self.loop)
            1/0

        # Test Future.__del__
        with mock.patch('asyncio.base_events.logger') as log:
            fut = asyncio.async(zero_error_coro(), loop=self.loop)
            fut.add_done_callback(lambda *args: self.loop.stop())
            self.loop.run_forever()
            fut = None # Trigger Future.__del__ or futures._TracebackLogger
            if PY34:
                # Future.__del__ in Python 3.4 logs error with
                # an actual exception context
                log.error.assert_called_with(
                    test_utils.MockPattern('.*exception was never retrieved'),
                    exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY))
            else:
                # futures._TracebackLogger logs only textual traceback
                log.error.assert_called_with(
                    test_utils.MockPattern(
                        '.*exception was never retrieved.*ZeroDiv'),
                    exc_info=False)
项目:annotated-py-asyncio    作者:hhstore    | 项目源码 | 文件源码
def test_log_slow_callbacks(self, m_logger):
        def stop_loop_cb(loop):
            loop.stop()

        @asyncio.coroutine
        def stop_loop_coro(loop):
            yield from ()
            loop.stop()

        asyncio.set_event_loop(self.loop)
        self.loop.set_debug(True)
        self.loop.slow_callback_duration = 0.0

        # slow callback
        self.loop.call_soon(stop_loop_cb, self.loop)
        self.loop.run_forever()
        fmt, *args = m_logger.warning.call_args[0]
        self.assertRegex(fmt % tuple(args),
                         "^Executing <Handle.*stop_loop_cb.*> "
                         "took .* seconds$")

        # slow task
        asyncio.async(stop_loop_coro(self.loop), loop=self.loop)
        self.loop.run_forever()
        fmt, *args = m_logger.warning.call_args[0]
        self.assertRegex(fmt % tuple(args),
                         "^Executing <Task.*stop_loop_coro.*> "
                         "took .* seconds$")