Taming blocking functions in asyncio results in error after some time #11936
Replies: 2 comments 1 reply
-
It will take me a little time to figure out what's going on, but I can state emphatically that the I think we need to step back and examine what you're trying to achieve. In general having one function polling the outcome of another is almost always a sign of sub-optimal |
Beta Was this translation helpful? Give feedback.
-
I have replicated your issue and "fixed" it with the commented line: async def foo():
global res
res = await unblock(slow_add, 1, 2, c = 3, d = 4)
async def main():
global res
res = None
bw = asyncio.create_task(busywork())
stopped = True
while True:
if stopped:
asyncio.create_task(foo())
stopped = False
else:
if res is not None:
print(f"\nDone. Result = {res}")
res = None
stopped = True
await asyncio.sleep_ms(10) # Makes the fault go away
await asyncio.sleep(0)
asyncio.run(main()) This looks like a timing issue with the (largely undocumented) I must stress that this kind of polling is not good practice, but without a clearer understanding of what you aim to achieve by using |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm following @peterhinch's great approach to tame blocking functions in asyncio.
Peter's example code:
Now, I wanted to execute the unblock function in the main repeatedly using the asyncio.create_task() without explicitly awaiting it, which at first sight works:
After some time though I get the following error:
When setting the sleep time in the busy_work function to smaller amounts the error occurs quicker... I tested this code first to see how it works. Eventually I want to process some data from sensors and do a calculation on the data in an unblocking way without blocking the main event loop. This approach seems very nice, but I don't understand why I get the error after some time in this modified example code. (FYI I'm running this code on a ESP32S3, MicroPython version v1.20.0)
Beta Was this translation helpful? Give feedback.
All reactions