-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetcher.py
24 lines (19 loc) · 830 Bytes
/
fetcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import aiohttp
from wiki import wiki_request
import asyncio
class WikiFetch(object):
"""
An asynchronous queue of workers listening for tasks from the WikiGraph.
Producers put work on the queue, while workers retrieve tasks and await
wiki_requests. Callbacks are fired upon response.
"""
def __init__(self):
self.to_fetch = asyncio.Queue()
async def worker(self):
async with aiohttp.ClientSession() as session:
while True:
title_to_fetch, cb, depth, is_source = await self.to_fetch.get()
resp = await wiki_request(session, title_to_fetch, is_source)
await cb(title_to_fetch, resp, depth, is_source)
async def producer(self, topic, cb, depth, is_source):
await self.to_fetch.put((topic, cb, depth, is_source))