Python asyncio.wrap_future() Examples
The following are 30
code examples of asyncio.wrap_future().
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: __init__.py From asyncqt with BSD 2-Clause "Simplified" License | 7 votes |
def run_in_executor(self, executor, callback, *args): """Run callback in executor. If no executor is provided, the default executor will be used, which defers execution to a background thread. """ self._logger.debug('Running callback {} with args {} in executor'.format(callback, args)) if isinstance(callback, asyncio.Handle): assert not args assert not isinstance(callback, asyncio.TimerHandle) if callback._cancelled: f = asyncio.Future() f.set_result(None) return f callback, args = callback.callback, callback.args if executor is None: self._logger.debug('Using default executor') executor = self.__default_executor if executor is None: self._logger.debug('Creating default executor') executor = self.__default_executor = QThreadExecutor() return asyncio.wrap_future(executor.submit(callback, *args))
Example #2
Source File: health.py From binderhub with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _get_pods(self): """Get information about build and user pods""" app_log.info("Getting pod statistics") k8s = self.settings["kubernetes_client"] pool = self.settings["executor"] get_user_pods = asyncio.wrap_future( pool.submit( k8s.list_namespaced_pod, self.settings["build_namespace"], label_selector="app=jupyterhub,component=singleuser-server", ) ) get_build_pods = asyncio.wrap_future( pool.submit( k8s.list_namespaced_pod, self.settings["build_namespace"], label_selector="component=binderhub-build", ) ) return await asyncio.gather(get_user_pods, get_build_pods)
Example #3
Source File: test_database.py From lbry-sdk with MIT License | 6 votes |
def test_fetchall_prevents_sqlite_misuse(self): # test that calling fetchall sufficiently avoids the race attempts = 0 def executemany_fetchall(query, params): self.db.executemany(query, params).fetchall() while attempts < self.max_misuse_attempts: f1 = asyncio.wrap_future( self.loop.run_in_executor( self.executor, executemany_fetchall, "update test1 set val='derp' where id=?", ((str(i),) for i in range(2)) ) ) f2 = asyncio.wrap_future( self.loop.run_in_executor( self.executor, executemany_fetchall, "update test2 set val='derp' where id=?", ((str(i),) for i in range(2)) ) ) attempts += 1 await asyncio.gather(f1, f2)
Example #4
Source File: app.py From binderhub with BSD 3-Clause "New" or "Revised" License | 6 votes |
def watch_build_pods(self): """Watch build pods Every build_cleanup_interval: - delete stopped build pods - delete running build pods older than build_max_age """ while True: try: await asyncio.wrap_future( self.executor.submit( lambda: Build.cleanup_builds( self.kube_client, self.build_namespace, self.build_max_age, ) ) ) except Exception: app_log.exception("Failed to cleanup build pods") await asyncio.sleep(self.build_cleanup_interval)
Example #5
Source File: connection.py From switchio with Mozilla Public License 2.0 | 6 votes |
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 #6
Source File: context.py From rssant with BSD 3-Clause "New" or "Revised" License | 6 votes |
def ask(self, dst, content=None, dst_node=None): """Send request and wait response""" if not dst_node: dst_node = self.registery.choice_dst_node(dst) msg = self.registery.create_message( dst=dst, is_ask=True, content=content, src=self.actor.name, dst_node=dst_node, ) if msg.is_local: future = ThreadFuture() msg.future = future if self.actor.is_async: return asyncio.wrap_future(future) else: return future.result() else: return self._actor_client.ask(msg)
Example #7
Source File: executor_service.py From federated with Apache License 2.0 | 6 votes |
def _Compute( self, request: executor_pb2.ComputeRequest, context: grpc.ServicerContext, ) -> executor_pb2.ComputeResponse: """Asynchronous implemention of `Compute`.""" py_typecheck.check_type(request, executor_pb2.ComputeRequest) try: value_id = str(request.value_ref.id) with self._lock: future_val = asyncio.wrap_future(self._values[value_id]) val = await future_val result_val = await val.compute() val_type = val.type_signature value_proto, _ = executor_service_utils.serialize_value( result_val, val_type) return executor_pb2.ComputeResponse(value=value_proto) except (ValueError, TypeError) as err: _set_invalid_arg_err(context, err) return executor_pb2.ComputeResponse()
Example #8
Source File: download.py From Nbdler with Apache License 2.0 | 6 votes |
def _await_loopsafe(self, *coros_or_futures): """ 事件循环安全的异步等待。 Args: *coros_or_futures: coroutine或future对象列表。 Returns: 返回coros_or_futures的返回结果列表。 """ current_loop = asyncio.get_running_loop() loop = self._loop if loop is None: loop = current_loop async def _execute_loop(): with h.enter(self._handlers): r = await asyncio.gather(*coros_or_futures) return r fut = asyncio.run_coroutine_threadsafe(_execute_loop(), loop) result = await asyncio.wrap_future(fut) return result
Example #9
Source File: mediawiki.py From oauthenticator with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get(self): consumer_token = ConsumerToken( self.authenticator.client_id, self.authenticator.client_secret, ) handshaker = Handshaker( self.authenticator.mw_index_url, consumer_token ) redirect, request_token = await wrap_future( self.authenticator.executor.submit(handshaker.initiate) ) self.set_secure_cookie( AUTH_REQUEST_COOKIE_NAME, jsonify(request_token), expires_days=1, path=url_path_join(self.base_url, 'hub', 'oauth_callback'), httponly=True) self.log.info('oauth redirect: %r', redirect) self.redirect(redirect)
Example #10
Source File: test_futures.py From android_universal with MIT License | 5 votes |
def test_wrap_future(self): def run(arg): return (arg, threading.get_ident()) ex = concurrent.futures.ThreadPoolExecutor(1) f1 = ex.submit(run, 'oi') f2 = asyncio.wrap_future(f1, loop=self.loop) res, ident = self.loop.run_until_complete(f2) self.assertTrue(asyncio.isfuture(f2)) self.assertEqual(res, 'oi') self.assertNotEqual(ident, threading.get_ident()) ex.shutdown(wait=True)
Example #11
Source File: utils.py From Nbdler with Apache License 2.0 | 5 votes |
def __iter__(self): yield from asyncio.wrap_future(self._task)
Example #12
Source File: test_futures.py From android_universal with MIT License | 5 votes |
def test_wrap_future_cancel(self): f1 = concurrent.futures.Future() f2 = asyncio.wrap_future(f1, loop=self.loop) f2.cancel() test_utils.run_briefly(self.loop) self.assertTrue(f1.cancelled()) self.assertTrue(f2.cancelled())
Example #13
Source File: test_futures.py From android_universal with MIT License | 5 votes |
def test_wrap_future_use_global_loop(self): with mock.patch('asyncio.futures.events') as events: events.get_event_loop = lambda: self.loop def run(arg): return (arg, threading.get_ident()) ex = concurrent.futures.ThreadPoolExecutor(1) f1 = ex.submit(run, 'oi') f2 = asyncio.wrap_future(f1) self.assertIs(self.loop, f2._loop) ex.shutdown(wait=True)
Example #14
Source File: test_futures.py From android_universal with MIT License | 5 votes |
def test_wrap_future_future(self): f1 = self._new_future(loop=self.loop) f2 = asyncio.wrap_future(f1) self.assertIs(f1, f2)
Example #15
Source File: utils.py From binderhub with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _submit(self, f, *args, **kwargs): return asyncio.wrap_future(self.executor.submit(f, *args, **kwargs))
Example #16
Source File: test_futures.py From android_universal with MIT License | 5 votes |
def test_wrap_future(self): f = DuckFuture() g = asyncio.wrap_future(f) assert g is f
Example #17
Source File: utils.py From Nbdler with Apache License 2.0 | 5 votes |
def ajoin(self): return await asyncio.wrap_future(self._task)
Example #18
Source File: utils.py From Nbdler with Apache License 2.0 | 5 votes |
def aget_loop(self): return await asyncio.wrap_future(self._loop)
Example #19
Source File: test_futures.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_wrap_future_future(self): f1 = asyncio.Future(loop=self.loop) f2 = asyncio.wrap_future(f1) self.assertIs(f1, f2)
Example #20
Source File: test_futures.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_wrap_future_use_global_loop(self): with mock.patch('asyncio.futures.events') as events: events.get_event_loop = lambda: self.loop def run(arg): return (arg, threading.get_ident()) ex = concurrent.futures.ThreadPoolExecutor(1) f1 = ex.submit(run, 'oi') f2 = asyncio.wrap_future(f1) self.assertIs(self.loop, f2._loop)
Example #21
Source File: test_futures.py From annotated-py-projects with MIT License | 5 votes |
def test_wrap_future_cancel2(self): f1 = concurrent.futures.Future() f2 = asyncio.wrap_future(f1, loop=self.loop) f1.set_result(42) f2.cancel() test_utils.run_briefly(self.loop) self.assertFalse(f1.cancelled()) self.assertEqual(f1.result(), 42) self.assertTrue(f2.cancelled())
Example #22
Source File: test_futures.py From annotated-py-projects with MIT License | 5 votes |
def test_wrap_future_cancel(self): f1 = concurrent.futures.Future() f2 = asyncio.wrap_future(f1, loop=self.loop) f2.cancel() test_utils.run_briefly(self.loop) self.assertTrue(f1.cancelled()) self.assertTrue(f2.cancelled())
Example #23
Source File: test_futures.py From annotated-py-projects with MIT License | 5 votes |
def test_wrap_future_use_global_loop(self, m_events): def run(arg): return (arg, threading.get_ident()) ex = concurrent.futures.ThreadPoolExecutor(1) f1 = ex.submit(run, 'oi') f2 = asyncio.wrap_future(f1) self.assertIs(m_events.get_event_loop.return_value, f2._loop)
Example #24
Source File: test_futures.py From annotated-py-projects with MIT License | 5 votes |
def test_wrap_future_future(self): f1 = asyncio.Future(loop=self.loop) f2 = asyncio.wrap_future(f1) self.assertIs(f1, f2)
Example #25
Source File: test_futures.py From annotated-py-projects with MIT License | 5 votes |
def test_wrap_future(self): def run(arg): return (arg, threading.get_ident()) ex = concurrent.futures.ThreadPoolExecutor(1) f1 = ex.submit(run, 'oi') f2 = asyncio.wrap_future(f1, loop=self.loop) res, ident = self.loop.run_until_complete(f2) self.assertIsInstance(f2, asyncio.Future) self.assertEqual(res, 'oi') self.assertNotEqual(ident, threading.get_ident())
Example #26
Source File: protocol.py From pygls with Apache License 2.0 | 5 votes |
def unregister_capability_async(self, params: UnregistrationParams): """Unregister a new capability on the client. Args: params(UnregistrationParams): UnregistrationParams from lsp specs callback(callable): Callabe which will be called after response from the client is received Returns: asyncio.Future object that will be resolved once a response has been received """ return asyncio.wrap_future(self.unregister_capability(params, None))
Example #27
Source File: protocol.py From pygls with Apache License 2.0 | 5 votes |
def register_capability_async(self, params: RegistrationParams): """Register a new capability on the client. Args: params(RegistrationParams): RegistrationParams from lsp specs callback(callable): Callabe which will be called after response from the client is received Returns: asyncio.Future object that will be resolved once a response has been received """ return asyncio.wrap_future(self.register_capability(params, None))
Example #28
Source File: protocol.py From pygls with Apache License 2.0 | 5 votes |
def get_configuration_async(self, params): """Calls `get_configuration` method but designed to use with coroutines Args: params(dict): ConfigurationParams from lsp specs Returns: asyncio.Future that can be awaited """ return asyncio.wrap_future(self.get_configuration(params, None))
Example #29
Source File: protocol.py From pygls with Apache License 2.0 | 5 votes |
def send_request_async(self, method, params=None): """Calls `send_request` and wraps `concurrent.futures.Future` with `asyncio.Future` so it can be used with `await` keyword. Args: method(str): The method name of the message to send params(any): The payload of the message Returns: `asyncio.Future` that can be awaited """ return asyncio.wrap_future(self.send_request(method, params))
Example #30
Source File: test_database.py From lbry-sdk with MIT License | 5 votes |
def test_unhandled_sqlite_misuse(self): # test SQLITE_MISUSE being incorrectly raised as a param 0 binding error attempts = 0 python_version = sys.version.split('\n')[0].rstrip(' ') try: while attempts < self.max_misuse_attempts: f1 = asyncio.wrap_future( self.loop.run_in_executor( self.executor, self.db.executemany, "update test1 set val='derp' where id=?", ((str(i),) for i in range(2)) ) ) f2 = asyncio.wrap_future( self.loop.run_in_executor( self.executor, self.db.executemany, "update test2 set val='derp' where id=?", ((str(i),) for i in range(2)) ) ) attempts += 1 await asyncio.gather(f1, f2) print(f"\nsqlite3 {sqlite3.version}/python {python_version} " f"did not raise SQLITE_MISUSE within {attempts} attempts of the race condition") self.assertTrue(False, 'this test failing means either the sqlite race conditions ' 'have been fixed in cpython or the test max_attempts needs to be increased') except sqlite3.InterfaceError as err: self.assertEqual(str(err), "Error binding parameter 0 - probably unsupported type.") print(f"\nsqlite3 {sqlite3.version}/python {python_version} raised SQLITE_MISUSE " f"after {attempts} attempts of the race condition")