鱼C论坛

 找回密码
 立即注册
查看: 1977|回复: 21

[已解决]Event loop is closed

[复制链接]
发表于 2021-8-13 14:09:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这段代码为什么会报题目的错误呢
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'
}
#/html/body/div[13]/div[1]/div[2]/dl/dd/a/img
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:
                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())
最佳答案
2021-8-13 16:56:30
本帖最后由 kogawananari 于 2021-8-13 16:57 编辑
import asyncio,requests,aiohttp
from lxml import etree
from threading import Thread
header = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}

def start_loop(loop):
    asyncio.set_event_loop(loop)
    loop.run_forever()

async def end_loop():
    loop = asyncio.get_event_loop()
    loop.stop()


async def download(url):
    name = url.rsplit('/', 1)[1]
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as res:
            async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
                await f.write(await res.content.read())


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')
    new_loop = asyncio.new_event_loop()
    t = Thread(target=start_loop, args=(new_loop,))
    t.start()
    for url in urls:
        asyncio.run_coroutine_threadsafe(download(url),new_loop)
    t.join()
我给你甩另一个线程里面貌似就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-13 14:29:17 | 显示全部楼层
需要加上这句
tasks = []
    for url in urls:
        task = asyncio.create_task(download(url))
        tasks.append(task)
    await asyncio.wait(tasks)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 14:57:00 | 显示全部楼层
asyncio.run是这样的 改成3.8之前的写法可以不报错。。还有就是  请求http而不是https也不会报这个错  bug属于是
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-13 15:17:58 | 显示全部楼层

我一开始就是用的create_task,报的也是这个错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-13 15:18:28 | 显示全部楼层
kogawananari 发表于 2021-8-13 14:57
asyncio.run是这样的 改成3.8之前的写法可以不报错。。还有就是  请求http而不是https也不会报这个错{:10_2 ...

抱歉我没看懂,能详细点嘛
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 15:22:51 | 显示全部楼层
一只魈咸鱼 发表于 2021-8-13 15:17
我一开始就是用的create_task,报的也是这个错

这样应该可以了
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'
}
# /html/body/div[13]/div[1]/div[2]/dl/dd/a/img


async def download(url):
    name = url.rsplit('/', 1)[1]
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as res:
            async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
                await f.write(await res.content.read())


async def main():
    tasks = []
    for url in urls:
        task = asyncio.create_task(download(url))
        tasks.append(task)
    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())
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 15:23:22 | 显示全部楼层
一只魈咸鱼 发表于 2021-8-13 15:17
我一开始就是用的create_task,报的也是这个错

在修改这两句
async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
                await f.write(await res.content.read())
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-13 15:31:19 | 显示全部楼层

还是不行,这是报错:
Task exception was never retrieved
future: <Task finished name='Task-16' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-15' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-14' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-13' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-12' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-11' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-10' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-9' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-8' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-7' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-6' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-5' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-4' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-3' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-2' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Task exception was never retrieved
future: <Task finished name='Task-17' coro=<download() done, defined at D:/MyPcharmProject/爬虫/爬取唯美壁纸.py:11> exception=AttributeError('__aexit__')>
Traceback (most recent call last):
  File "D:/MyPcharmProject/爬虫/爬取唯美壁纸.py", line 15, in download
    async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
AttributeError: __aexit__
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000198DF849430>
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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 16:45:35 | 显示全部楼层
import asyncio
import aiohttp
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):
    async with aiohttp.ClientSession() as session:
        async with session.get(url=url, headers=headers) as response:
            page_text = await response.text()
            print(page_text[30:])

async def main():
    tasks = []
    for url in urls:
        task = asyncio.create_task(download(url))
        tasks.append(task)
    await asyncio.wait(tasks)
if __name__ == '__main__':
    urls = [
        'https://fishc.com.cn/forum.php?mod=viewthread&tid=200766&page=1#pid5515666',
        'https://fishc.com.cn/forum.php?mod=viewthread&tid=200766&page=1#pid5515666'
    ]
    asyncio.run(main())

你试试看 哪怕是 请求你这个贴子页面 依然会报错的 但是我开个http 127.0.0.1的服务去自己请求自己就不会报错 99%是aiohttp这个库有问题不能请求https
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 16:56:30 | 显示全部楼层    本楼为最佳答案   
本帖最后由 kogawananari 于 2021-8-13 16:57 编辑
import asyncio,requests,aiohttp
from lxml import etree
from threading import Thread
header = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}

def start_loop(loop):
    asyncio.set_event_loop(loop)
    loop.run_forever()

async def end_loop():
    loop = asyncio.get_event_loop()
    loop.stop()


async def download(url):
    name = url.rsplit('/', 1)[1]
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as res:
            async with open(f'彼岸桌面壁纸下载/{name}', 'wb') as f:
                await f.write(await res.content.read())


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')
    new_loop = asyncio.new_event_loop()
    t = Thread(target=start_loop, args=(new_loop,))
    t.start()
    for url in urls:
        asyncio.run_coroutine_threadsafe(download(url),new_loop)
    t.join()
我给你甩另一个线程里面貌似就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-13 17:25:05 | 显示全部楼层
kogawananari 发表于 2021-8-13 16:45
你试试看 哪怕是 请求你这个贴子页面 依然会报错的 但是我开个http 127.0.0.1的服务去自己请求自己就不 ...

那对于https咋请求哎,现在感觉大多数都是https
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 17:28:45 | 显示全部楼层
一只魈咸鱼 发表于 2021-8-13 17:25
那对于https咋请求哎,现在感觉大多数都是https

试一下我刚才写的 甩给另一个线程好像就不报错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-13 17:30:40 | 显示全部楼层
kogawananari 发表于 2021-8-13 17:28
试一下我刚才写的 甩给另一个线程好像就不报错了


嗯是的,其实我还想知道https的该怎么搞,这么整也太麻烦了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 17:31:51 | 显示全部楼层
一只魈咸鱼 发表于 2021-8-13 17:30
嗯是的,其实我还想知道https的该怎么搞,这么整也太麻烦了

那就甩到其他线程嘛
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-13 17:32:35 | 显示全部楼层
kogawananari 发表于 2021-8-13 16:56
我给你甩另一个线程里面貌似就可以了

不过为什么会想到再开一个线程呢,主线程也没被占用呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 17:34:22 | 显示全部楼层
一只魈咸鱼 发表于 2021-8-13 17:32
不过为什么会想到再开一个线程呢,主线程也没被占用呀

join比较稳   主线程有提前结束的嫌疑
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-13 17:36:10 | 显示全部楼层
kogawananari 发表于 2021-8-13 17:34
join比较稳   主线程有提前结束的嫌疑

好滴吧我现在是听不懂了,不过还是谢谢你!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 18:20:39 From FishC Mobile | 显示全部楼层
一只魈咸鱼 发表于 2021-8-13 17:36
好滴吧我现在是听不懂了,不过还是谢谢你!

就是asyncio.run这个还是里面自动get loop 和 close loop 。它close的时候https请求还没完就close就报这个错。而asyncio.run是py3.8以上的 我在另一个线程会被迫用的3.8以下的写法 loop run forever 这个方法是永远等待你去抛给它新任task,所以它不会自动调用loop close。所以更稳。所以我说99%是aiohttp问题 1%是3.8以上语法问题。不用其他线程也可以 loop拿着了自己close就行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-13 20:13:13 | 显示全部楼层
kogawananari 发表于 2021-8-13 18:20
就是asyncio.run这个还是里面自动get loop 和 close loop 。它close的时候https请求还没完就close就报这 ...

意思asyncio.run有bug吗,它close早了是吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-13 22:48:14 | 显示全部楼层
一只魈咸鱼 发表于 2021-8-13 20:13
意思asyncio.run有bug吗,它close早了是吗

这俩都有bug 不然也不会是https才会close早
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-13 19:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表