For the past 2 weeks, I have been using the Wokwi simulator to learn micropython and preparing some code before I get my real esp32s. I have been suspecting a bug in the simulator with uasyncio for quite a while. Indeed, I felt like the uasyncio.sleep(n) and uasyncio.sleep_ms(n) where quite slow and were actually causing problems with timing (as if there was a lot of overhead) and therefore, the sleep time given was not really respected.
I tested the same function, with and without the uasyncio sleep functions and noticed quite a big difference between it and the synchronous sleep in the simulator. Today, I finally got my Esp32s devkit and tested it with the code below:
Code: Select all
#necessary imports
async def benchmark(n):
l1 = Pin(12)
on = l1.on
off = l1.off
for i in range(n):
on()
off()
await asyncio.sleep_ms(5)
n = 500
s = ticks_ms()
asyncio.run(benchmark(n))
print(f"{n} loops ran in {ticks_diff(ticks_ms(), s)}ms")
- Wokwi printed: 500 loops ran in 3126ms
- Real ESP32 printed: 500 loops ran in 2501ms
This is exactly what we get from the real ESP32 + a few ms taken to blink the led.
However, with Wikwo we find a 3126/2500 = 1,25 <=> 25% increase in the expected run time!
If someone knows why there are such discrepancies, I would be glad to hear it. Otherwise I hope this remark was constructive and can help people understand why they may have timing issues with asyncio
PS: this may have been causing issues observed in this thread where it seems I could never reach the sampling rate i wanted, whatever the number of sensors/rate I set! viewtopic.php?f=18&t=12452