跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • Python asyncio 问题
未分類
13 5 月 2020

Python asyncio 问题

Python asyncio 问题

資深大佬 : fangwenxue 3

    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)

  • 資深大佬 : sikong31

    1 唯一的 ClientSession,就自己定义一个 AppSession 类,返回一个类属性就是了
    2 再调用就相当于阻塞了,你可以参考下 asyncio 动态添加任务
    4 如果不是等最后一起处理,列表是没办法顺序返回的,因为哪个请求先返回并不是你这边控制的,只有结果全部返回了才是顺序的

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具