python程式碼錯誤RuntimeError: Session is closed
我在使用aiohttp庫和asyncio庫進行協程下載圖片時報出錯誤RuntimeError: Session is closed
我的程式碼如下
import aiohttp
import asyncio
async def fetch(session,url):
print('開始下載圖片:',url)
async with session.get(url,verify_ssl=False)as response:
conent = await response.content.read()
file_name = url[-16:]
with open(file_name,mode='wb')as fp:
fp.write(conent)
async def main():
async with aiohttp.ClientSession() as session:
url_list = [
'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=231146236,2859633049&fm=26&gp=0.jpg',
'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2194951532,1309127690&fm=26&gp=0.jpg',
'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1602084713475&di=20def259a9a44f66c1eed626732e67a2&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Fdc54564e9258d1090c1b7aead158ccbf6d814dea.jpg',
]
tasks = [asyncio.create_task(fetch(session,url))for url in url_list]
await asyncio.wait(tasks)
if __name__ == '__main__':
asyncio.run(main())
然後就一直報出錯誤:RuntimeError: Session is closed
後來去網上查資料發現這個錯誤應該不是伺服器的問題,而是自己程式碼塊有問題,後來我仔細看了一下自己的程式碼,發現是因為我的縮排出現了問題
然後正確的程式碼應該是
import aiohttp
import asyncio
async def fetch(session,url):
print('開始下載圖片:',url)
async with session.get(url,verify_ssl=False)as response:
conent = await response.content.read()
file_name = url[-16:]
with open(file_name,mode='wb')as fp:
fp.write(conent)
async def main():
async with aiohttp.ClientSession() as session:
url_list = [
'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=231146236,2859633049&fm=26&gp=0.jpg',
'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2194951532,1309127690&fm=26&gp=0.jpg',
'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1602084713475&di=20def259a9a44f66c1eed626732e67a2&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Fdc54564e9258d1090c1b7aead158ccbf6d814dea.jpg',
]
tasks = [asyncio.create_task(fetch(session,url))for url in url_list]
await asyncio.wait(tasks)
if __name__ == '__main__':
asyncio.run(main())
執行:
相關文章
- PyTorch出現錯誤“RuntimeError: Found dtype Double but expected Float”PyTorchError
- MySQL 錯誤程式碼MySql
- Windows 錯誤程式碼Windows
- HTTP錯誤程式碼HTTP
- WSA 錯誤程式碼
- Idea啟動專案報{dataSource-1} closed錯誤Idea
- Python 指令碼中呼叫 Java 程式時 Classpath 錯誤Python指令碼Java
- matplotlib使用時報錯RuntimeError: Python is not installed as a framework(一)ErrorPythonFramework
- VS錯誤程式碼列
- MySQL錯誤程式碼大全MySql
- http 錯誤程式碼表HTTP
- V$SESSION記錄的BLOCKING_SESSION錯誤SessionBloC
- client backup was not attempted because backup window closed 錯誤解決一例client
- MySQL:錯誤程式碼:2059MySql
- 錯誤程式碼的設計!
- mysql錯誤程式碼對照表MySql
- pb錯誤程式碼詳解
- Python程式設計者共性錯誤Python程式設計
- PostgreSQLjdbc錯誤程式碼對映(SQLSTATE)SQLJDBC
- 常見 HTTP 錯誤程式碼大全HTTP
- MySql 常見錯誤程式碼大全MySql
- 線上壓縮程式碼-定位錯誤
- 錯誤程式碼 ILLEGAL_PARTNER_EXTERFACE
- IIS 錯誤程式碼分析手冊
- 錯誤記錄(八)could not initialize proxy - no SessionSession
- ORA-600(krvxdds: duplicated session not)錯誤Session
- MySQL常見錯誤程式碼及程式碼說明MySql
- Python 編碼錯誤的本質原因Python
- 【ERROR】Oracle列印錯誤程式碼解釋ErrorOracle
- 爬蟲錯誤程式碼如何解決?爬蟲
- Windows 8啟用錯誤程式碼解析Windows
- 22、listener的幾個錯誤程式碼
- onvif soap 協議的錯誤程式碼協議
- kill session遇到ORA-00031錯誤Session
- Python 程式碼除錯技巧Python除錯
- Python Template 錯誤Python
- 解決python中文編碼錯誤問題Python
- Python 程式碼除錯—使用 pdb 除錯Python除錯