
def crawl(self, tasks: list, task_handler=None, callback=None, semaphore_count=100): if not task_handler: task_handler = self.page_handler main_loop = asyncio.new_event_loop() asyncio.set_event_loop(main_loop) semaphore = asyncio.Semaphore(semaphore_count) n = len(tasks) asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) new_tasks = [] async def _run(): async with semaphore: async with ClientSession() as session: for i, task in enumerate(tasks): future = asyncio.ensure_future(task_handler(task, i=i + 1, n=n, session=session)) if callback: future.add_done_callback(callback) new_tasks.append(future) return await asyncio.gather(*new_tasks) try: result = main_loop.run_until_complete(_run()) return [x for j in result for x in j] except Exception as e: logging.exception(e) finally: if not main_loop.is_closed(): main_loop.close() return [] async def page_handler(self, task, session, **kwargs): ''' 省略部分代码 ''' result = self.crawl(book_item_url_list, self.item_handler) return result 怎样创建一个唯一 ClientSession()
在 page_handler 再次条用 crawl 出现
我传了 callback 又用了 return 这是不是又点矛盾
我打印 i/n 这个 i 是乱序 怎样任务列表顺序执行
1 sikong31 2020-05-11 11:11:52 +08:00 1 唯一的 ClientSession,就自己定义一个 AppSession 类,返回一个类属性就是了 2 再调用就相当于阻塞了,你可以参考下 asyncio 动态添加任务 4 如果不是等最后一起处理,列表是没办法顺序返回的,因为哪个请求先返回并不是你这边控制的,只有结果全部返回了才是顺序的 |