Mixing asynchio with synchronous (blocking) code #10762
Replies: 3 comments 9 replies
-
|
Blocking methods are incompatible with Incidentally |
Beta Was this translation helpful? Give feedback.
-
|
The MFRC522 read functionality doesn't block, it just tries for quite a long time. After all a card reader that blocked wouldn't be much use but trying for a while makes sense. If you look at the MFRC522 code in the If your other elements are time critical and require very frequent processing you may still need to follow the threading route. |
Beta Was this translation helpful? Give feedback.
-
|
Since the driver is a .py, you could try to yield control at some points inserting |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have developed some asyncio classes that play well together. LoRa and queues and an OLED display all play nicely together. Then I invited class MFRC522 to the party (https://github.com/Lennyz1988/micropython-mfrc522) and it refuses to share processing time.
I think I am expecting that something that shuttles among processing logic that can await should be able to wrap a synchronous piece inside asynchronous task and "share". Apparently not.
First I would ask for a bit of education: My reading indicates that asyncio.create_task combined in a gather SHOULD each receive some processing time. This is not the case. When I launch the program below, the part using MFRC522 class comes up and you can see the security_loop messages appear. rfid_lop reads the card and sits. Issuing a ctrl-C allows the security_loop to process:
I had started with :
queues = QUEUES()
lora = LORA(cfg, queues)
display = OLED(cfg, queues)
rfid = RFID(cfg, queues)
security = SecurityPosts(cfg, queues, display)
And also tried this aproach:
import asyncio
import time
class SynchronousClass:
def do_something(self):
print("Doing something synchronously...")
time.sleep(5)
print("Done.")
async def async_function():
synchronous_object = SynchronousClass()
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, synchronous_object.do_something)
async def main():
await async_function()
but is seems
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
The example is malformed (loo?)
Beta Was this translation helpful? Give feedback.
All reactions