Python Forum
A question about 'Event loop is closed'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A question about 'Event loop is closed'
#1
I will appreciate it if anyone could give me some help!
My code:
import asyncio
import aiohttp
from lxml import etree
import requests
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
async def download(url):
    name = url.rsplit('/',1)[1]
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as res:
            with open(f'彼岸桌面壁纸下载/{name}','wb') as f: # '彼岸壁纸下载' is a directory I builded before.
                f.write(await res.content.read())
async def main():
    tasks=[]
    for url in urls:
        tasks.append(download(url))
    await asyncio.wait(tasks)
if __name__ == '__main__':
    url0 = 'https://www.enterdesk.com/zhuomianbizhi/'
    text = requests.get(url0).text
    tree = etree.HTML(text)
    urls = tree.xpath('/html/body/div[13]/div[1]/div/dl/dd/a/img/@src')
    asyncio.run(main())
Error:
Error:
D:\MyPcharmProject\venv\Scripts\python.exe D:/MyPcharmProject/爬虫/Umei.py Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000023AD6F1C670> Traceback (most recent call last): File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 116, in __del__ self.close() File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 108, in close self._loop.call_soon(self._call_connection_lost, None) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 711, in call_soon self._check_closed() File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 504, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed Process finished with exit code 0
It seem that pictures have been captured. However, I can't make this error disappear even though I use try-except! Huh
Reply
#2
Have you used asyncio before? I would expect to see code setting up an event loop and tasks added to the event loop.
Reply
#3
(Oct-04-2021, 04:04 PM)deanhystad Wrote: Have you used asyncio before? I would expect to see code setting up an event loop and tasks added to the event loop.

Yeah...But in the new vision, we can omit loop.create_task and put list which contains coroutine function into asyncio.wait directly. And then, use asyncio.run instead get_event_loop and loop.close, isn't it? If there is anything wrong, I'm willing to be refuted.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [closed] check whether an integer is 32 or 64 bits paul18fr 4 226 May-27-2024, 04:55 PM
Last Post: deanhystad
  This result object does not return rows. It has been closed automatically dawid294 6 1,483 Mar-30-2024, 03:08 AM
Last Post: NolaCuriel
  help RuntimeError: no running event loop marpaslight 5 3,980 Oct-18-2022, 10:04 PM
Last Post: marpaslight
Exclamation question about input, while loop, then print jamie_01 5 2,799 Sep-30-2021, 12:46 PM
Last Post: Underscore
  bleak library RuntimeError: This event loop is already running alice93 3 4,251 Sep-30-2021, 08:06 AM
Last Post: alice93
  ValueError: I/O operation on closed file problem aliwien 0 2,174 Apr-23-2021, 05:50 PM
Last Post: aliwien
  for loop question KEYS 1 1,797 Oct-27-2020, 11:42 PM
Last Post: jefsummers
  Netmiko Loop question sc00ter 2 3,412 Oct-24-2020, 10:54 PM
Last Post: sc00ter
  RuntimeError: This event loop is already running newbie2019 2 7,039 Sep-30-2020, 06:59 PM
Last Post: forest44
  while loop question KEYS 2 2,098 Sep-26-2020, 11:02 PM
Last Post: KEYS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020