How to use async/await in Python 3.5?

Running coroutines requires an event loop. Use the asyncio() library to create one: import asyncio # Python 3.7+ asyncio.run(foo()) or # Python 3.6 and older loop = asyncio.get_event_loop() loop.run_until_complete(foo()) Also see the Tasks and Coroutines chapter of the asyncio documentation. If you already have a loop running, you’d want to run additional coroutines concurrently by … Read more

When to use and when not to use Python 3.5 `await` ?

By default all your code is synchronous. You can make it asynchronous defining functions with async def and “calling” these functions with await. A More correct question would be “When should I write asynchronous code instead of synchronous?”. Answer is “When you can benefit from it”. In cases when you work with I/O operations as … Read more

How to use asyncio with existing blocking library?

There are (sort of) two questions here: how can I run blocking code asynchronously within a coroutine how can I run multiple async tasks at the “same” time (as an aside: asyncio is single-threaded, so it is concurrent, but not truly parallel). Concurrent tasks can be created using the high-level asyncio.create_task or the low-level asyncio.ensure_future. … Read more

Using abc.ABCMeta in a way it is compatible both with Python 2.7 and Python 3.5

You could use six.add_metaclass or six.with_metaclass: import abc, six @six.add_metaclass(abc.ABCMeta) class SomeAbstractClass(): @abc.abstractmethod def do_something(self): pass six is a Python 2 and 3 compatibility library. You can install it by running pip install six or by downloading the latest version of six.py to your project directory. For those of you who prefer future over six, … Read more

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

The SIGSEGV signal indicates a “segmentation violation” or a “segfault”. More or less, this equates to a read or write of a memory address that’s not mapped in the process. This indicates a bug in your program. In a Python program, this is either a bug in the interpreter or in an extension module being … Read more

Debugger times out at “Collecting data…”

I had the same issue with Pycharm 2018.2 when working on a complex Flask project with SocketIO. When I put a debug breakpoint inside the code and pressed the debug button, it stopped at the breakpoint, but the variables didn’t load. It was just infinitely collecting data. I enabled Gevent compatibility and it resolved the … Read more

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)