python async 异步

1. requests 包不支持异步,使用 requests_async

import asyncio
import threading
import time
from aiohttp import ClientSession
import requests_async

# 数仓特征url
dw_pre_url = "xxx" 
dw_pre_url_2 = "xxx"
dw_pre_url_3 = "xxx"
# 实时特征url
tec_pre_url = "xxx"

order_no = 'aaa'
contact_mob = 'bbb'


async def get_dw_await2(order_no):
    async with ClientSession() as session:
        async with session.post(url=dw_pre_url_2 + order_no) as response:
            # a = [i for i in range(10000000)]
            # post 时长很短,可以异步,但是如果是费时的操作,则需要加多线程
            print('Hello World:%s' % time.time())
            t = threading.currentThread()
            print('Thread id : %d' % t.ident)
            # await asyncio.sleep(1)
            return await response.json()


async def get_dw_await3(order_no):
    async with ClientSession() as session:
        async with session.post(url=dw_pre_url_3 + order_no) as response:
            # a = [i for i in range(10000000)]
            print('Hello World:%s' % time.time())
            t = threading.currentThread()
            print('Thread id : %d' % t.ident)
            return await response.json()

async def get_dw_await4(contact_mob):
    async with ClientSession() as session:
        async with session.post(url=dw_pre_url_3 + contact_mob) as response:
            # a = [i for i in range(10000000)]
            print('Hello World:%s' % time.time())
            t = threading.currentThread()
            print('Thread id : %d' % t.ident)
            return await response.json()


async def get_tec(orders):
    if orders == '':
        return None
    session = aiohttp.ClientSession()
    headers = {'Content-Type': 'application/json'}
    payload = {"order_no": orders}
    response = await session.post(tec_pre_url, headers=headers, data=json.dumps(payload))
    result = await response.json()
    await session.close()
    return result


def run():
    start = time.time()
    task1 = asyncio.ensure_future(get_dw_await2(order_no))
    task2 = asyncio.ensure_future(get_dw_await3(order_no))
    task3 = asyncio.ensure_future(get_dw_await4(contact_mob))
    tasks = [task1, task2, task3]
    result = loop.run_until_complete(asyncio.gather(*tasks))
    print(result)
    print(len(result))
    print(time.time() - start)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    run()

2. 普通的异步

import asyncio
import time
import traceback
from abc import ABCMeta, abstractmethod
import requests
import json
import aiohttp
import threading

async def get_dw_await1():
    a = [i for i in range(10000000)]
    t = threading.currentThread()
    print('Thread id : %d' % t.ident)
    print('Hello World1:%s' % time.time())
    await asyncio.sleep(4)
    return a


async def get_dw_await2():
    a = [i for i in range(10000000)]
    t = threading.currentThread()
    print('Thread id : %d' % t.ident)
    print('Hello World2:%s' % time.time())
    return a


async def get_dw_await3():
    a = [i for i in range(10000000)]
    t = threading.currentThread()
    print('Thread id : %d' % t.ident)
    print('Hello World3:%s' % time.time())
    return a


def run():
    start = time.time()
    task1 = asyncio.ensure_future(get_dw_await1())
    task2 = asyncio.ensure_future(get_dw_await2())
    task3 = asyncio.ensure_future(get_dw_await3())
    tasks = [task1, task2, task3]
    result = loop.run_until_complete(asyncio.gather(*tasks))
    print(len(result))
    print(time.time() - start)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    run()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值