Skip to content
Advertisement

Tag: python-asyncio

async exec in python

I’d like to call exec in an async function and do something like the following code (which is not valid): More precisely, I’d like to be able to wait for a future inside the code that runs in exec. How can this be achieved? Answer Note: F-strings are only supported in python 3.6+. For older versions, use %s, .format() or

Semantic equivalent of async for

From the docs relative to async for syntax in Python 3.5, I gathered that it was introduced to iterate over an awaitable iterator. There is something I don’t get in the semantic equivalent that follow the description though: What is the line iter = type(iter).__aiter__(iter) doing? Why is it necessary? Answer Magic methods in python, e.g. __add__, are always looked

How to set class attribute with await in __init__

How can I define a class with await in the constructor or class body? For example what I want: or example with class body attribute: My solution (But I would like to see a more elegant way) Answer Most magic methods aren’t designed to work with async def/await – in general, you should only be using await inside the dedicated

asyncio: Wait for event from other thread

I’m designing an application in Python which should access a machine to perform some (lengthy) tasks. The asyncio module seems to be a good choice for everything that is network-related, but now I need to access the serial port for one specific component. I’ve implemented kind of an abstraction layer for the actual serial port stuff, but can’t figure out

Mocking async call in python 3.5

How do I mock async call from one native coroutine to other one using unittest.mock.patch? I currently have quite an awkward solution: Then This works but looks ugly. Is there more pythonic way to do this? Answer The solution was actually quite simple: I just needed to convert __call__ method of mock into coroutine: This works perfectly, when mock is

Advertisement