V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
fangwenxue
V2EX  ›  问与答

Python asyncio 问题

  •  
  •   fangwenxue · 2020-05-10 22:11:49 +08:00 · 802 次点击
    这是一个创建于 1445 天前的主题,其中的信息可能已经有所发展或是发生改变。
        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()

      • session = ClientSession() 这样不好使
    • 在 page_handler 再次条用 crawl 出现

      • Cannot run the event loop while another loop is running
      • crawl 里面的任务会在产生子任务, 这个子任务如何用 loop 加入任务列表
    • 我传了 callback 又用了 return 这是不是又点矛盾

    • 我打印 i/n 这个 i 是乱序 怎样任务列表顺序执行

    1 条回复    2020-05-11 11:11:52 +08:00
    sikong31
        1
    sikong31  
       2020-05-11 11:11:52 +08:00
    1 唯一的 ClientSession,就自己定义一个 AppSession 类,返回一个类属性就是了
    2 再调用就相当于阻塞了,你可以参考下 asyncio 动态添加任务
    4 如果不是等最后一起处理,列表是没办法顺序返回的,因为哪个请求先返回并不是你这边控制的,只有结果全部返回了才是顺序的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3400 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:45 · PVG 19:45 · LAX 04:45 · JFK 07:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.