Skip to content
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

Limit coroutines using a pool instead of chunks #1544

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/environment-friends.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies:
- flake8
- black
- google-cloud-core
- google-cloud-storage
- google-api-core
- google-api-python-client
- httpretty
Expand Down
44 changes: 31 additions & 13 deletions fsspec/asyn.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,38 @@ async def _run_coros_in_chunks(
batch_size = len(coros)

assert batch_size > 0

async def _run_coro(coro):
try:
return await asyncio.wait_for(coro, timeout=timeout)
except Exception as e:
if not return_exceptions:
raise
return e
finally:
callback.relative_update(1)

coros = iter(coros)
stop = False
pending = set()
results = []
for start in range(0, len(coros), batch_size):
chunk = [
asyncio.Task(asyncio.wait_for(c, timeout=timeout))
for c in coros[start : start + batch_size]
]
if callback is not DEFAULT_CALLBACK:
[
t.add_done_callback(lambda *_, **__: callback.relative_update(1))
for t in chunk
]
results.extend(
await asyncio.gather(*chunk, return_exceptions=return_exceptions),
)

while pending or not stop:
while len(pending) < batch_size and not stop:
try:
coro = next(coros)
except StopIteration:
stop = True
else:
pending.add(asyncio.ensure_future(_run_coro(coro)))

if not pending:
break
martindurant marked this conversation as resolved.
Show resolved Hide resolved

done, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
martindurant marked this conversation as resolved.
Show resolved Hide resolved
while done:
results.append(await done.pop())

return results


Expand Down
Loading