11

import httpx
import re
import asyncio
import pandas as pd
import time
url = "https://www.baidu.com"

cookie = {
        "Cookie": "aaa"
    }

headers = {
    # "Cookie": cookie,
    "Host": "portal.cn-shanghai-scge-d01.sls.res.scgecloud.com",
    "Referer": "https://portal.cn-shanghai-scge-d01.sls.res.scgecloud.com/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
}

def get_start2from(url):
    ret = re.search(".*&from=(?P<from>\d+).*to=(?P<to>\d+).*", url)
    fm = ret.group("from")
    to = ret.group("to")
    return fm, to

def get_total_record_num(fm, to):
    url = "https://portal.cn-shanghai-scge-d01.sls.res.scgecloud.com/user/data/projects/ali-beaver-net-log/logstores/odl_beaver_log_tcp_session/histograms?_=0.30244826620225274&from=%s&line=10&logStoreName=odl_beaver_log_tcp_session&offset=10&projectName=ali-beaver-net-log&timeType=-1&to=%s&topic=" % (
        fm, to)
    ret = httpx.get(url=url, headers=headers, verify=False)
    if ret.status_code == 200 and ret.json().get("count"):
        return ret.json().get("count")
    else:
        print("获取记录总数出错,请检查")


async def send_request(client,fm,to,offset):
    url_model =  "https://www.baidu.com % (
    fm, offset, to)
    ret = await client.get(url=url)
    print(f"{offset//100}开始了,响应吗是{ret.status_code}")
    #返回一个列表[{数据1},{数据2}]
    return ret.json().get("logs")

async def main():
    async with httpx.AsyncClient(headers=headers,cookies=cookie,verify=False) as client:
        task = [send_request(client,fm,to,i*100) for i in range(200)]
        done,pending = await asyncio.wait(task)
        e = []
        for i in done:
            e.extend(i.result())
        return e

fm,to = get_start2from(url)
print(fm,to)

https://mp.weixin.qq.com/s?__biz=MzI2OTQ1NzEyMQ==&mid=2247483826&idx=1&sn=7d59de1421121f5712cbdba761ea1404&chksm=eae1423cdd96cb2a56fc28c7b916369d4a11aa7
https://www.zhihu.com/search?type=content&q=asyncio 把返回值添加列表
https://www.cnblogs.com/blueberry-mint/p/15250125.html

import asyncio

async def follow2(t):
    await asyncio.sleep(2)
    #这里的t是test函数的运行返回值,是一个task运行完的对象,result方法可以获取返回值
    print(t.result())

def follow(t):
    #这里的t是test函数的运行返回值,是一个task运行完的对象,result方法可以获取返回值
    print(t.result())
    # print(f"from follow,参数是{done.result}")

async def test(i):
    print(f"from test,开始阻塞,传入的参数是{i}")
    await asyncio.sleep(1)
    print(f"from test,停止阻塞,传入的参数是{i}")
    return f"test的返回结果{i}"
obj = asyncio.get_event_loop()
# t1 = test("t1") #和普通函数不一样,这一步函数并不会执行,需要循环器开始执行
# t2 = test("t2")
# obj.run_until_complete(asyncio.wait([t1,t2],timeout=6))

# t1 = obj.create_task(test("t1"))
# t1.add_done_callback(follow)



t2 = asyncio.ensure_future(test("t2"))
t = [t1,t2]

#循环器可以传任务,任务可以用wait和gather来并发执行
#wait返回pending和done,gather只返回执行结果
b = asyncio.wait(t,timeout=4)
done,pending =obj.run_until_complete(b)
# c = asyncio.gather(t1,t2)
# ret =obj.run_until_complete(c)
# print(ret)
# print(done)
# print(pending)


"""
协程三要素
1.函数前加要用async 表示一个协程函数
2. 对于阻塞的函数或者代码块用await 进行阻塞挂起
3. 把协程函数丢到循环器执行
    3.1循环器先实例化
    3.2 调用循环器的run_until_complete
    3.3 循环器放入的是future对象,不是

https://www.javaroad.cn/questions/356414
"""
上一篇:解决MVC EF Code First错误:Model compatibility cannot be checked because the EdmMetadata type was not included in the model.


下一篇:[剑指offer] 第二层