asyncio: Is it possible to cancel a future been run by an Executor?

In this case, there is no way to cancel the Future once it has actually started running, because you’re relying on the behavior of concurrent.futures.Future, and its docs state the following: cancel() Attempt to cancel the call. If the call is currently being executed and cannot be cancelled then the method will return False, otherwise … Read more

How to call an async function without await?

One way would be to use create_task function: import asyncio async def handler_message(request): … loop = asyncio.get_event_loop() loop.create_task(perform_message(x,y,z)) … As per the loop documentation, starting Python 3.10, asyncio.get_event_loop() is deprecated. If you’re trying to get a loop instance from a coroutine/callback, you should use asyncio.get_running_loop() instead. This method will not work if called from the … Read more

asyncio.sleep() vs time.sleep() in Python

You aren’t seeing anything special because there’s nothing much asynchronous work in your code. However, the main difference is that time.sleep(5) is blocking, and asyncio.sleep(5) is non-blocking. When time.sleep(5) is called, it will block the entire execution of the script and it will be put on hold, just frozen, doing nothing. But when you call … Read more

What does “SSLError: [SSL] PEM lib (_ssl.c:2532)” mean using the Python ssl library?

Assuming that version 3.6 is being used: See: https://github.com/python/cpython/blob/3.6/Modules/_ssl.c#L3523-L3534 PySSL_BEGIN_ALLOW_THREADS_S(pw_info.thread_state); r = SSL_CTX_check_private_key(self->ctx); PySSL_END_ALLOW_THREADS_S(pw_info.thread_state); if (r != 1) { _setSSLError(NULL, 0, __FILE__, __LINE__); goto error; } What it is saying is that SSL_CTX_check_private_key failed; thus, the private key is not correct. Reference to the likely version: https://github.com/python/cpython/blob/3.4/Modules/_ssl.c#L2529-L2535

How to use ‘yield’ inside async function?

Upd: Starting with Python 3.6 we have asynchronous generators and able to use yield directly inside coroutines. import asyncio async def async_generator(): for i in range(3): await asyncio.sleep(1) yield i*i async def main(): async for i in async_generator(): print(i) loop = asyncio.get_event_loop() try: loop.run_until_complete(main()) finally: loop.run_until_complete(loop.shutdown_asyncgens()) # see: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.shutdown_asyncgens loop.close() Old answer for Python 3.5: … Read more

How can I wrap a synchronous function in an async coroutine?

Eventually I found an answer in this thread. The method I was looking for is run_in_executor. This allows a synchronous function to be run asynchronously without blocking an event loop. In the sleep example I posted above, it might look like this: import asyncio from time import sleep async def sleep_async(loop, delay): # None uses … Read more

What kind of problems (if any) would there be combining asyncio with multiprocessing?

You should be able to safely combine asyncio and multiprocessing without too much trouble, though you shouldn’t be using multiprocessing directly. The cardinal sin of asyncio (and any other event-loop based asynchronous framework) is blocking the event loop. If you try to use multiprocessing directly, any time you block to wait for a child process, … Read more