Asyncio vs. Gevent [closed]

“Simple” answer from real-world usage: Good thing about gevent — you can patch things, which means that you [theoretically] can use synchronous libraries. I.e. you can patch django. Bad thing about gevent — not everything can be patched, if you must use some DB driver that can’t be patched, you’re doomed Worst thing about gevent … Read more

Make a Python asyncio call from a Flask route

You can incorporate some async functionality into Flask apps without having to completely convert them to asyncio. import asyncio from flask import Flask async def abar(a): print(a) loop = asyncio.get_event_loop() app = Flask(__name__) @app.route(“https://stackoverflow.com/”) def notify(): loop.run_until_complete(abar(“abar”)) return “OK” if __name__ == “__main__”: app.run(debug=False, use_reloader=False) This will block the Flask response until the async function … 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

What’s the correct way to clean up after an interrupted event loop?

When you CTRL+C, the event loop gets stopped, so your calls to t.cancel() don’t actually take effect. For the tasks to be cancelled, you need to start the loop back up again. Here’s how you can handle it: import asyncio @asyncio.coroutine def shleepy_time(seconds): print(“Shleeping for {s} seconds…”.format(s=seconds)) yield from asyncio.sleep(seconds) if __name__ == ‘__main__’: loop … Read more

python-asyncio TypeError: object dict can’t be used in ‘await’ expression

Only asynchronous (defined with async def) functions can be awaited. Whole idea is that such functions are written special way what makes possible to run (await) them without blocking event loop. If you want to get result from common (defined with def) function that takes some considerable time to be executed you have these options: … Read more

How to combine python asyncio with threads?

It’s pretty simple to delegate a method to a thread or sub-process using BaseEventLoop.run_in_executor: import asyncio import time from concurrent.futures import ProcessPoolExecutor def cpu_bound_operation(x): time.sleep(x) # This is some operation that is CPU-bound @asyncio.coroutine def main(): # Run cpu_bound_operation in the ProcessPoolExecutor # This will make your coroutine block, but won’t block # the event … Read more

Mocking async call in python 3.5

The solution was actually quite simple: I just needed to convert __call__ method of mock into coroutine: class AsyncMock(MagicMock): async def __call__(self, *args, **kwargs): return super(AsyncMock, self).__call__(*args, **kwargs) This works perfectly, when mock is called, code receives native coroutine Example usage: @mock.patch(‘my.path.asyncio.sleep’, new_callable=AsyncMock) def test_stuff(sleep): # code

How to call a async function contained in a class?

Finally I could find the right way to do it (special thanks to @dirn) #!/usr/bin/env python3 import sys, json import asyncio from websockets import connect class EchoWebsocket: async def __aenter__(self): self._conn = connect(‘wss://ws.binaryws.com/websockets/v3’) self.websocket = await self._conn.__aenter__() return self async def __aexit__(self, *args, **kwargs): await self._conn.__aexit__(*args, **kwargs) async def send(self, message): await self.websocket.send(message) async def … Read more