Skip to content

The iterator return from aiofiles.os.scandir() is blocking, may block event loop #225

Description

@9-2-1

aiofiles.os.scandir() is asynchronous while the returned iterator is not. Iterating the iterator may block asyncio event loop.

import time
import asyncio
import aiofiles
large_folder_in_slow_disk = "H:\photos"
use_list = True
async def main():
    entries_iter = await aiofiles.os.scandir()
    if use_list:
        print(f"start_time: {time.monotonic():>10.5f}")
        entries = list(entries_iter) # It's a Iterator, not AsyncIterator, may block while iterating
        print(f"end_time: {time.monotonic():>10.5f}")
        # during the time asyncio event loop may helt
    else:
        print(f"start_time: {time.monotonic():>10.5f}")
            for entry in entries_iter: # Not async
                print(f"iter: {time.monotonic():>10.5f}")
                # can notice that iterating consume time

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions