I’ve read every post I could find about how to gracefully handle a script with an asyncio event loop getting terminated with Ctrl-C, and I haven’t been able to get any of them to work without printing one or more tracebacks as I do so. The answers are pretty much all over the place, and I haven’t been able implement
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
Python asyncio task list generation without executing the function
While working in asyncio, I’m trying to use a list comprehension to build my task list. The basic form of the function is as follows: My goal is to use a list of terms to create my task list: My initial thought was: This doesn’t create the task list it runs the function during the list comprehension. Is there a
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
What kind of problems (if any) would there be combining asyncio with multiprocessing?
As almost everyone is aware when they first look at threading in Python, there is the GIL that makes life miserable for people who actually want to do processing in parallel – or at least give it a chance. I am currently looking at implementing something like the Reactor pattern. Effectively I want to listen for incoming socket connections on