Skip to content

Commit

Permalink
wrap as_complete with async (#1350)
Browse files Browse the repository at this point in the history
Originally as_complete is not defined with async, the asyncio.Semaphore
is created in main thread not in asynci.run. This will cause 'There is
no event loop' error when:
1. len(coros) is more than max_workers; 
2. run 'evalute' function at the second time and nest_asyncio is not
applied.
  • Loading branch information
liupgd authored Sep 24, 2024
1 parent 0a538e4 commit 5ad4943
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/ragas/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def is_event_loop_running() -> bool:
return loop.is_running()


def as_completed(coros, max_workers):
async def as_completed(coros, max_workers):
if max_workers == -1:
return asyncio.as_completed(coros)

Expand Down Expand Up @@ -103,7 +103,7 @@ def results(self) -> t.List[t.Any]:
async def _aresults() -> t.List[t.Any]:
results = []
for future in tqdm(
futures_as_they_finish,
await futures_as_they_finish,
desc=self.desc,
total=len(self.jobs),
# whether you want to keep the progress bar after completion
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async def echo_order(index: int):

async def _run():
results = []
for t in as_completed([echo_order(1), echo_order(2), echo_order(3)], 3):
for t in await as_completed([echo_order(1), echo_order(2), echo_order(3)], 3):
r = await t
results.append(r)
return results
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_executor_in_jupyter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"\n",
"async def _run():\n",
" results = []\n",
" for t in as_completed([echo_order(1), echo_order(2), echo_order(3)], 3):\n",
" for t in await as_completed([echo_order(1), echo_order(2), echo_order(3)], 3):\n",
" r = await t\n",
" results.append(r)\n",
" return results\n",
Expand Down

0 comments on commit 5ad4943

Please sign in to comment.