亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python 協程運行正常,卻提示ClientSession.close錯誤

python 協程運行正常,卻提示ClientSession.close錯誤

qq_亂序_0 2019-04-12 16:56:37
本地搭建的Flask測試環境:協程代碼如下:import?asyncio import?aiohttp import?time start?=?time.time() async?def?get(url): ????session?=?aiohttp.ClientSession() ????response?=?await?session.get(url) ????result?=?await?response.text() ????session.close() ????return?result async?def?request(): ????url?=?'http://127.0.0.1:5000' ????print('Waiting?for',?url) ????result?=?await?get(url) ????print('Get?response?from',?url,?'Result:',?result) tasks?=?[asyncio.ensure_future(request())?for?_?in?range(5)] loop?=?asyncio.get_event_loop() loop.run_until_complete(asyncio.wait(tasks)) end?=?time.time() print('Cost?time:',?end?-?start)運行結果如下:Waiting?for?http://127.0.0.1:5000Waiting?for?http://127.0.0.1:5000Waiting?for?http://127.0.0.1:5000Waiting?for?http://127.0.0.1:5000Waiting?for?http://127.0.0.1:5000Get?response?from?http://127.0.0.1:5000?Result:?Hello!Get?response?from?http://127.0.0.1:5000?Result:?Hello!Get?response?from?http://127.0.0.1:5000?Result:?Hello!Get?response?from?http://127.0.0.1:5000?Result:?Hello!C:/Users/gyp/PycharmProjects/untitled/4545/xiechengp.py:24:?RuntimeWarning:?coroutine?'ClientSession.close'?was?never?awaited??session.close()Unclosed?client?sessionclient_session:?<aiohttp.client.ClientSession?object?at?0x0000000003B102E8>Unclosed?client?sessionclient_session:?<aiohttp.client.ClientSession?object?at?0x0000000003B10940>Unclosed?client?sessionclient_session:?<aiohttp.client.ClientSession?object?at?0x00000000030ADC50>Unclosed?client?sessionclient_session:?<aiohttp.client.ClientSession?object?at?0x00000000030ADC18>Get?response?from?http://127.0.0.1:5000?Result:?Hello!Cost?time:?3.013000011444092Unclosed?client?sessionclient_session:?<aiohttp.client.ClientSession?object?at?0x0000000003B10DD8>
查看完整描述

1 回答

?
pardon110

TA貢獻1038條經驗 獲得超227個贊

將get協程內的return 改為yield

查看完整回答
反對 回復 2019-04-12
  • 1 回答
  • 0 關注
  • 4442 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號