栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 并发之asyncio

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 并发之asyncio

直接上代码

import requests
import time
import asyncio
import aiohttp
import random

a = 0

url = 'http://97.64.124.57/api/user/get_filter_conditions'  # worker 1
url = 'http://97.64.120.30:8080/api/user/get_filter_conditions'  # worker 2
url = 'https://www.eosask.com/api/user/get_filter_conditions'
url = 'http://97.64.33.19/api/user/get_filter_conditions'  # nginx

# url = 'https://www.cryptocpi.com/api/user/get_filter_conditions'
# url = 'https://www.baidu.com'
#url = 'https://bwh1.net/'
def create_phone():
    # 第二位数字
    second = [3, 4, 5, 7, 8][random.randint(0, 4)]

    # 第三位数字
    third = {
        3: random.randint(0, 9),
        4: [5, 7, 9][random.randint(0, 2)],
        5: [i for i in range(10) if i != 4][random.randint(0, 8)],
        7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)],
        8: random.randint(0, 9),
    }[second]

    # 最后八位数字
    suffix = random.randint(9999999, 100000000)

    # 拼接手机号
    return "86" + "1{}{}{}".format(second, third, suffix)


start = time.time()


async def post_test(_):

    async with aiohttp.ClientSession() as session:
        phone = create_phone()

        # async with session.post('https://www.eosask.com/api/user/register',
        #               data={'username': phone,
        #                     'password':  "",
        #                     're_password':  "",
        #                     'code': 111111}) as resp:
        async with session.get(url) as resp:
            global a
            a = a + 1
            print(resp.status, a)
            #print(await resp.text())

tasks = [asyncio.ensure_future(post_test(_)) for _ in range(500)]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))

# for i in range(100):
#     requests.get(url)
#     print(i)

end = time.time()
print('Cost time:', end - start)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/983141.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号