当前位置:首页 > 日记本 > 正文内容

python asyncio要选择使用 windows的事件循环策略

zhangchap2年前 (2022-03-30)日记本458
# -*- coding:utf-8 -*-
import aiohttp
import asyncio
import platform

# 事件循环策略有关,windows 需要选择使用 windows的事件循环策略,注意W是大写
if 'Windows' in platform.platform():
    policy = asyncio.WindowsSelectorEventLoopPolicy()
    asyncio.set_event_loop_policy(policy)


# 或者使用
# import sys
# if sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith('win'):
#     asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get('http://www.huanghepiao.com') as response:
            print('Status',response.status)
            print('Content-type:',response.headers['content-type'])
            html = await response.text()
            print('Body:',html[:300],'...')

if __name__ == '__main__':
    asyncio.run(main())


分享给朋友:

相关文章

lxml win 安装方法

文档地址:https://lxml.de/installation.htmlwindows 下安装不了的都可以在这里找:https://www.lfd.uci.edu/~gohl...

宝塔重启服务器后,Redis就启动不了解决方案

宝塔重启服务器后,Redis就启动不了解决方案

1.更改权限 chown -R redis.redis /www/server/redis/ 2.设置持久化...

python使用mongodb数据库

from pymongo import MongoClient,collection class KSpdier(Thread):   ...

python 随机生成时间戳写入txt文件/运行sql语句

import time from random import randint with open('time.txt', ...

python 获取当前时间及随机时间戳

import time from random import randint time.strftime('%Y-%m-%d %H:%M:...

python读取txt文件放到Queue队列

from queue import Queue with open('kw.txt',encoding='utf-8')&nb...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。