Skip to content

Commit

Permalink
fifo gather
Browse files Browse the repository at this point in the history
  • Loading branch information
yindaheng98 committed Dec 18, 2023
1 parent 3d5e924 commit ec5e726
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
29 changes: 29 additions & 0 deletions dblp_crawler/gather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import asyncio


async def gather(*coros_or_futures):
queue = asyncio.Queue()

async def task(coro_or_future):
result = await coro_or_future
await queue.put(result)

tasks = [task(c) for c in coros_or_futures]
task_sum = asyncio.gather(*tasks)
for _ in range(len(tasks)):
yield await queue.get()
await task_sum

if __name__ == "__main__":
import random

async def main():
async def wait(n):
await asyncio.sleep(n)
return n
tasks = [wait(n) for n in range(10)]
random.shuffle(tasks)
async for n in gather(*tasks):
print(n)

asyncio.run(main())
5 changes: 3 additions & 2 deletions dblp_crawler/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Optional, Iterable, AsyncIterable, List

from dblp_crawler import download_person, DBLPPerson, Publication, download_journal_list, JournalList
from .gather import gather

logger = logging.getLogger("graph")

Expand Down Expand Up @@ -44,7 +45,7 @@ async def init_persons_from_journals(self) -> None:
atasks.append(self._collect_authors(publication)) # 遍历这个文章
logger.debug(str(publication))
asc, afc = 0, 0
for publication, success, fail in await asyncio.gather(*atasks):
async for publication, success, fail in gather(*atasks):
asc += success
afc += fail
yield publication
Expand Down Expand Up @@ -117,7 +118,7 @@ async def _bfs_once(self):
total_publication_count += publication_count

# 执行任务:获取已有作者列表中的论文
for publication, success, fail in await asyncio.gather(*atasks):
async for publication, success, fail in gather(*atasks):
total_author_succ_count += success
total_author_fail_count += fail
yield publication
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

setup(
name='dblp_crawler',
version='2.0.1',
version='2.1',
author='yindaheng98',
author_email='[email protected]',
url='https://github.com/yindaheng98/dblp-crawler',
Expand Down

0 comments on commit ec5e726

Please sign in to comment.