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

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 请问下面异步嵌套的 await 为什么会导致 mongo 入库失败
未分類
20 11 月 2020

请问下面异步嵌套的 await 为什么会导致 mongo 入库失败

请问下面异步嵌套的 await 为什么会导致 mongo 入库失败

資深大佬 : yagamil 5

请问下面异步嵌套的 await 为什么会导致 Task <Task pending coro=<fetch() running at xxxx.py:45> cb=[gather.<locals>._done_callback() 回调失败 ?

加入了异步插入 mongo 后导致的,去掉入库那一段就没问题。

 mongo_client = AsyncIOMotorClient(connect_uri) collection = mongo_client['db_stock']['new_stock_ttjj']  def parse_json(content):    # 解析     content += ';function getV(){return hsEnHLwG;}'     ctx = execjs.compile(content)     result = ctx.call('getV')     return result  async def update_data(data):    # 异步 mongo 入库      code = data['securitycode']     found =  await collection.find_one({'securitycode':code})     if not found:         await collection.insert_one(data)   async def fetch(session,page):      async with session.get(home_url.format(page),headers=headers) as resp:         content = await resp.text()          try:             js_content = parse_json(content)             for stock_info in js_content['data']:                 securityshortname = stock_info['securityshortname']                  print(securityshortname)                 await update_data(stock_info)         except Exception as e:             print(e)   async def main():     async with aiohttp.ClientSession() as session:         async with session.get(home_url.format(1), headers=headers) as resp:              content = await resp.text()             js_data = parse_json(content)             pages = js_data['pages']             tasks =[]             for page in range(1,pages+1):                 task = asyncio.ensure_future(fetch(session,page))                 tasks.append(task)              await asyncio.gather(*tasks)  asyncio.run(main())  

大佬有話說 (4)

  • 資深大佬 : keepeye

    task = asyncio.ensure_future(fetch(session,page))
    似乎可以改成
    task = fetch(session,page)

  • 主 資深大佬 : yagamil

    @keepeye 问题不在这里。

  • 主 資深大佬 : yagamil

    找到原因:
    def run(main, *, debug=False):
    if events._get_running_loop() is not None:
    raise RuntimeError(
    “asyncio.run() cannot be called from a running event loop”)

    if not coroutines.iscoroutine(main):
    raise ValueError(“a coroutine was expected, got {!r}”.format(main))

    loop = events.new_event_loop()

    asyncio.run 内部定义了一个 loop,
    moto 初始化的时候内部也用了这个。

    用 loop 定义,motor 的定义放在 loop 后面。

  • 資深大佬 : n37r09u3

    放后面是用同一个 loop 了 吧?

文章導覽

上一篇文章
下一篇文章

AD

其他操作

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

51la

4563博客

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