python如何在多线程中使用异步

这是一个异步爬虫,上代码

    headers = {
   
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
    async def fetch(url, semaphore):
        async with semaphore:
            async with aiohttp.ClientSession() as session:
                try:
                    async with session.get(url, headers=headers, timeout=10) as response:
                        # proxies = {"http": "http://10.10.1.10:3128","https": "http://10.10.1.10:1080"}
                        # async with session.get(url, headers=headers, timeout=10,proxy=proxies) as response:#代理IP设置
                        return await response.text(), url
                except:
                    return """<html><head><title>Error</title></head></html>""", url
    async def main():
        urls=[]#网址自定义
        semaphore = asyncio.Semaphore(500)#设置默认并发数500,在windows中最大为512, Linux中限制为1024
        tasks = [fetch(url,semaphore) for url in urls]
        responses = await asyncio.gather(*tasks)
        for response in responses:
            print(response[0],response[1])#可以在这里处置返回的网址数据

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

将其放进Threading线程的时候会报错

  • 错误提示1:RuntimeError: There is no current event loop in thread ‘Thread-1’.
  • 使用网上的方法说是添加
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

https://www.codenong.com/48725890/

  • 也会报错提示:AttributeError: module ‘asyncio‘ has no attribute ‘WindowsSelectorEventLoopPolicy‘ 。
  • 可能是版本问题导致吧,使用的版本是python3.6.7版本。

解决方法

将调用代码

        loop = asyncio.get_event_loop()#获取线程事件
        loop.run_until_complete(main())#调用

替换如下:

        loop = asyncio.new_event_loop()#新建一个线程事件
        asyncio.set_event_loop(loop)#设置线程事件
        loop.run_until_complete(main())#调用

分析可能是由于直接调用线程事件和threading.Thread冲突了

        t1=Reptile_Thread()
        t1.start()
        print("运行")
class Reptile_Thread(threading.Thread):
    """网站爬取线程"""
    def __init__(self,parent=None):
        super(Reptile_Thread, self).__init__(parent)
    def run(self):
    	    headers = {
   
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
    async def fetch(url, semaphore):
        async with semaphore:
            async with aiohttp.ClientSession() as session:
                try:
                    async with session.get(url, headers=headers, timeout=10) as response:
                        # proxies = {"http": "http://10.10.1.10:3128","https": "http://10.10.1.10:1080"}
                        # async with session.get(url, headers=headers, timeout=10,proxy=proxies) as response:#代理IP设置
                        return await response.text(), url
                except:
                    return """<html><head><title>Error</title></head></html>""", url
    async def main():
        urls=[]#网址自定义
        semaphore = asyncio.Semaphore(500)#设置默认并发数500,在windows中最大为512, Linux中限制为1024
        tasks = [fetch(url,semaphore) for url in urls]
        responses = await asyncio.gather(*tasks)
        for response in responses:
            print(response[0],response[1])#可以在这里处置返回的网址数据
    #使用一下方法可以解决该错误。
    loop = asyncio.new_event_loop()#新建一个线程事件
    asyncio.set_event_loop(loop)#设置线程事件
    loop.run_until_complete(main())#调用

相关推荐

  1. python如何线使用异步

    2023-12-09 20:40:04       57 阅读
  2. python线使用

    2023-12-09 20:40:04       52 阅读
  3. 如何Python实现线进程?

    2023-12-09 20:40:04       32 阅读
  4. 如何C#实现线

    2023-12-09 20:40:04       33 阅读
  5. LinkedList 线如何使用

    2023-12-09 20:40:04       59 阅读
  6. Python线是什么

    2023-12-09 20:40:04       51 阅读
  7. Python----线使用

    2023-12-09 20:40:04       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-09 20:40:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-09 20:40:04       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-09 20:40:04       87 阅读
  4. Python语言-面向对象

    2023-12-09 20:40:04       96 阅读

热门阅读

  1. ssh隧道转发

    2023-12-09 20:40:04       61 阅读
  2. 使用c#罗列、监视、控制进程

    2023-12-09 20:40:04       56 阅读
  3. Pytorch中的resize和reshape

    2023-12-09 20:40:04       54 阅读
  4. [原创] FPGA的JTAG烧录不稳定或烧录失败原因分析

    2023-12-09 20:40:04       68 阅读
  5. CCF-走迷宫(bfs)

    2023-12-09 20:40:04       57 阅读