-
Notifications
You must be signed in to change notification settings - Fork 1k
Async functions should not call open() #704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks @cclauss -- The advice is of course correct, and for Our VFS layer just has no concept of asyncio-style concurrency, and to make this work it would need to go all the way down to the filesystem driver (which for both FAT and LittleFS is third-party code). You could get some way there using a thread pool (like In most cases where the device is using a filesystem on internal flash (or memory-mapped external flash like ESP32), opening-for-reading and reading of flash-backed files is just a memory-mapped operation. There is no "waiting" so there isn't anything to be gained anyway. For writing on the other hand, the erase and write block times are significant, however in the case where the filesystem is backed by the same flash memory that the MicroPython firmware itself is running out of, the CPU is paused during these operations anyway (and this applies to both cores on e.g. rp2040). So I think for now the |
Awesome write up. Quite helpful. For micropython, it is too bad that ruff puts |
%
ruff --select=ASYNC .
I have used https://pypi.org/project/aiofiles elsewhere to fix this but I am unsure of its compatibility with micropython.
%
ruff rule ASYNC101
open-sleep-or-subprocess-in-async-function (ASYNC101)
Derived from the flake8-async linter.
What it does
Checks that async functions do not contain calls to
open
,time.sleep
,or
subprocess
methods.Why is this bad?
Blocking an async function via a blocking call will block the entire
event loop, preventing it from executing other tasks while waiting for the
call to complete, negating the benefits of asynchronous programming.
Instead of making a blocking call, use an equivalent asynchronous library
or function.
Example
Use instead:
@jimmo
The text was updated successfully, but these errors were encountered: