Python asyncio.async() Examples
The following are 30
code examples of asyncio.async().
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: conversationwidget.py From qhangups with GNU General Public License v3.0 | 6 votes |
def set_active(self): """Activate conversation tab""" settings = QtCore.QSettings() # Set the client as active if settings.value("send_client_active", True, type=bool): future = asyncio.async(self.client.set_active()) future.add_done_callback(lambda future: future.result()) # Mark the newest event as read if settings.value("send_read_state", True, type=bool): future = asyncio.async(self.conv.update_read_timestamp()) future.add_done_callback(lambda future: future.result()) self.num_unread_local = 0 self.set_title() self.messageTextEdit.setFocus()
Example #2
Source File: test_tasks.py From annotated-py-projects with MIT License | 6 votes |
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)
Example #3
Source File: test_aio.py From python-consul with MIT License | 6 votes |
def test_kv_subscribe(self, loop, consul_port): c = consul.aio.Consul(port=consul_port, loop=loop) @asyncio.coroutine def get(): fut = asyncio.async(put(), loop=loop) index, data = yield from c.kv.get('foo') assert data is None index, data = yield from c.kv.get('foo', index=index) assert data['Value'] == six.b('bar') yield from fut c.close() @asyncio.coroutine def put(): yield from asyncio.sleep(1.0/100, loop=loop) response = yield from c.kv.put('foo', 'bar') assert response is True loop.run_until_complete(get())
Example #4
Source File: test_aio.py From python-consul with MIT License | 6 votes |
def test_kv_missing(self, loop, consul_port): c = consul.aio.Consul(port=consul_port, loop=loop) @asyncio.coroutine def main(): fut = asyncio.async(put(), loop=loop) yield from c.kv.put('index', 'bump') index, data = yield from c.kv.get('foo') assert data is None index, data = yield from c.kv.get('foo', index=index) assert data['Value'] == six.b('bar') yield from fut c.close() @asyncio.coroutine def put(): yield from asyncio.sleep(2.0/100, loop=loop) yield from c.kv.put('foo', 'bar') loop.run_until_complete(main())
Example #5
Source File: test_tasks.py From annotated-py-projects with MIT License | 6 votes |
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()
Example #6
Source File: test_tasks.py From annotated-py-projects with MIT License | 6 votes |
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)
Example #7
Source File: test_base_events.py From annotated-py-projects with MIT License | 6 votes |
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()
Example #8
Source File: example.py From aioimaplib with GNU General Public License v3.0 | 6 votes |
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()
Example #9
Source File: test_tasks.py From annotated-py-projects with MIT License | 6 votes |
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)
Example #10
Source File: epycyzm.py From epycyzm with MIT License | 6 votes |
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)
Example #11
Source File: PSHandler.py From Harness with MIT License | 6 votes |
def client_connected_handler(self, client_reader, client_writer): remote_conn_info = client_writer.get_extra_info("peername") local_conn_info = client_writer.get_extra_info("sockname") SID = self.add_session(remote_conn_info, local_conn_info, stype="asyncio_session") # Start a new asyncio.Task to handle this specific client connection task = asyncio.async(self.handle_client(SID, client_reader, client_writer)) def client_done(SID, _): # When the tasks that handles the specific client connection is done client_writer.close() self.remove_session(SID) # Add the client_done callback to be run when the future becomes done task.add_done_callback(functools.partial(client_done, SID))
Example #12
Source File: client.py From jarvis with GNU General Public License v2.0 | 6 votes |
def deliver_message(self, timeout=None): """ Deliver next received message. Deliver next message received from the broker. If no message is available, this methods waits until next message arrives or ``timeout`` occurs. This method is a *coroutine*. :param timeout: maximum number of seconds to wait before returning. If timeout is not specified or None, there is no limit to the wait time until next message arrives. :return: instance of :class:`hbmqtt.session.ApplicationMessage` containing received message information flow. :raises: :class:`asyncio.TimeoutError` if timeout occurs before a message is delivered """ deliver_task = ensure_future(self._handler.mqtt_deliver_next_message(), loop=self._loop) self.client_tasks.append(deliver_task) self.logger.debug("Waiting message delivery") done, pending = yield from asyncio.wait([deliver_task], loop=self._loop, return_when=asyncio.FIRST_EXCEPTION, timeout=timeout) if deliver_task in done: self.client_tasks.pop() return deliver_task.result() else: #timeout occured before message received deliver_task.cancel() raise asyncio.TimeoutError
Example #13
Source File: cron.py From moxie with MIT License | 6 votes |
def __call__(self): self.logger = CronService.resolve("moxie.cores.log.LogService") self.run = CronService.resolve("moxie.cores.run.RunService") self.database = CronService.resolve("moxie.cores.database.DatabaseService") while True: jobs = (yield from self.database.job.list( Job.manual == False, Job.scheduled <= ( dt.datetime.utcnow() + dt.timedelta(seconds=self.HEARTBEAT)) )) # yield from self.logger.log("cron", "Wakeup") for job in jobs: asyncio.async(self.handle(job)) # yield from self.logger.log("cron", "Sleep") yield from asyncio.sleep(self.HEARTBEAT)
Example #14
Source File: main.py From netwrok-server with MIT License | 6 votes |
def run(): mp = None if config["MAIL"]["START_MAILER"]: mp = Popen(['python3', mailer]) try: start_server = websockets.serve(server.server, config["SERVER"]["INTERFACE"], config["SERVER"]["PORT"]) if config["SERVER"]["RELOAD_ON_CHANGE"]: asyncio.async(reloader(mp)) loop = asyncio.get_event_loop() asyncio.async(start_server) if config["IPN"]["START_IPN_SERVER"]: asyncio.async(ipn.init(loop)) ext = config["SERVER"].get("EXT", None) if ext is not None: load_extensions(ext) loop.run_forever() finally: if mp is not None: mp.terminate()
Example #15
Source File: client.py From jarvis with GNU General Public License v2.0 | 5 votes |
def _do_connect(self): return_code = yield from self._connect_coro() self._disconnect_task = ensure_future(self.handle_connection_close(), loop=self._loop) return return_code
Example #16
Source File: test_tasks.py From annotated-py-projects with MIT License | 5 votes |
def test_yield_future_passes_cancel(self): # Cancelling outer() cancels inner() cancels waiter. proof = 0 waiter = asyncio.Future(loop=self.loop) @asyncio.coroutine def inner(): nonlocal proof try: yield from waiter except asyncio.CancelledError: proof += 1 raise else: self.fail('got past sleep() in inner()') @asyncio.coroutine def outer(): nonlocal proof try: yield from inner() except asyncio.CancelledError: proof += 100 # Expect this path. else: proof += 10 f = asyncio.async(outer(), loop=self.loop) test_utils.run_briefly(self.loop) f.cancel() self.loop.run_until_complete(f) self.assertEqual(proof, 101) self.assertTrue(waiter.cancelled())
Example #17
Source File: test_tasks.py From annotated-py-projects with MIT License | 5 votes |
def test_async_neither(self): with self.assertRaises(TypeError): asyncio.async('ok')
Example #18
Source File: client_handler.py From jarvis with GNU General Public License v2.0 | 5 votes |
def handle_write_timeout(self): try: if not self._ping_task: self.logger.debug("Scheduling Ping") self._ping_task = ensure_future(self.mqtt_ping()) except BaseException as be: self.logger.debug("Exception ignored in ping task: %r" % be)
Example #19
Source File: broker.py From jarvis with GNU General Public License v2.0 | 5 votes |
def _broadcast_loop(self): running_tasks = deque() try: while True: while running_tasks and running_tasks[0].done(): running_tasks.popleft() broadcast = yield from self._broadcast_queue.get() if self.logger.isEnabledFor(logging.DEBUG): self.logger.debug("broadcasting %r" % broadcast) for k_filter in self._subscriptions: if broadcast['topic'].startswith("$") and (k_filter.startswith("+") or k_filter.startswith("#")): self.logger.debug("[MQTT-4.7.2-1] - ignoring brodcasting $ topic to subscriptions starting with + or #") elif self.matches(broadcast['topic'], k_filter): subscriptions = self._subscriptions[k_filter] for (target_session, qos) in subscriptions: if 'qos' in broadcast: qos = broadcast['qos'] if target_session.transitions.state == 'connected': self.logger.debug("broadcasting application message from %s on topic '%s' to %s" % (format_client_message(session=broadcast['session']), broadcast['topic'], format_client_message(session=target_session))) handler = self._get_handler(target_session) task = ensure_future( handler.mqtt_publish(broadcast['topic'], broadcast['data'], qos, retain=False), loop=self._loop) running_tasks.append(task) else: self.logger.debug("retaining application message from %s on topic '%s' to client '%s'" % (format_client_message(session=broadcast['session']), broadcast['topic'], format_client_message(session=target_session))) retained_message = RetainedApplicationMessage( broadcast['session'], broadcast['topic'], broadcast['data'], qos) yield from target_session.retained_messages.put(retained_message) except CancelledError: # Wait until current broadcasting tasks end if running_tasks: yield from asyncio.wait(running_tasks, loop=self._loop)
Example #20
Source File: broker.py From jarvis with GNU General Public License v2.0 | 5 votes |
def publish_session_retained_messages(self, session): self.logger.debug("Publishing %d messages retained for session %s" % (session.retained_messages.qsize(), format_client_message(session=session)) ) publish_tasks = [] handler = self._get_handler(session) while not session.retained_messages.empty(): retained = yield from session.retained_messages.get() publish_tasks.append(ensure_future( handler.mqtt_publish( retained.topic, retained.data, retained.qos, True), loop=self._loop)) if publish_tasks: yield from asyncio.wait(publish_tasks, loop=self._loop)
Example #21
Source File: spinner_asyncio.py From notebooks with MIT License | 5 votes |
def supervisor(): # <7> spinner = asyncio.async(spin('thinking!')) # <8> print('spinner object:', spinner) # <9> result = yield from slow_function() # <10> spinner.cancel() # <11> return result
Example #22
Source File: test_base_events.py From annotated-py-projects with MIT License | 5 votes |
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$")
Example #23
Source File: test_base_events.py From annotated-py-projects with MIT License | 5 votes |
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)
Example #24
Source File: test_windows_events.py From annotated-py-projects with MIT License | 5 votes |
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()
Example #25
Source File: 13_07_basic_async.py From Python_Master-the-Art-of-Design-Patterns with MIT License | 5 votes |
def five_sleepers(): print("Creating five tasks") tasks = [asyncio.async(random_sleep(i)) for i in range(5)]
Example #26
Source File: alert.py From moxie with MIT License | 5 votes |
def _emit(self, flavor, **kwargs): kwargs['type'] = flavor for handler in self.callbacks: asyncio.async(handler(kwargs))
Example #27
Source File: core.py From butterfield with BSD 3-Clause "New" or "Revised" License | 5 votes |
def ws_handler(self, url, handler): self.ws = yield from websockets.connect(url) self.running = True # Fix keepalives as long as we're ``running``. asyncio.async(self.ws_keepalive()) while True: content = yield from self.ws.recv() if content is None: break message = json.loads(content) if 'ok' in message: continue message_type = message['type'] type_handlers = self.handlers[message_type] for handler in itertools.chain(self.handlers[ALL], type_handlers): asyncio.async(handler(self, message)) self.running = False
Example #28
Source File: ptproxy.py From ptproxy with MIT License | 5 votes |
def handle_client(client_reader, client_writer): host, port = CFG['server'].rsplit(':', 1) try: remote_reader, remote_writer = yield from proxied_connection( (host, int(port)), *CFG['_ptcli']) except aiosocks.SocksError as ex: print(logtime(), ex) print(logtime(), 'WARNING: Please check the config and the log of PT.') return asyncio.async(proxy_data(client_reader, remote_writer)) asyncio.async(proxy_data(remote_reader, client_writer))
Example #29
Source File: steemasyncclient.py From piston-lib with MIT License | 5 votes |
def _initialize(self, coroutines): if self._witness_ws: l = yield from self.login.login(self._config.witness["user"], self._config.witness["password"]) if not l: raise RPCClientError("Could not login to steemd node") for (alias, api_name) in self._config.witness["apis"].items(): api_id = yield from self.login.get_api_by_name("{}_api".format(api_name)) if not api_id: raise RPCClientError("Could not acquire {}_api".format(api_name)) self._api_id[api_name] = api_id self._api_map[alias] = api_name futures = [] for c in coroutines: futures.append(asyncio.async(c(self))) yield from asyncio.wait(futures)
Example #30
Source File: irc.py From CloudBot with GNU General Public License v3.0 | 5 votes |
def connection_lost(self, exc): self._connected = False # create a new connected_future for when we are connected. self._connected_future = asyncio.Future(loop=self.loop) if exc is None: # we've been closed intentionally, so don't reconnect return logger.error("[{}] Connection lost: {}".format(self.conn.name, exc)) asyncio.async(self.conn.connect(), loop=self.loop)