Asynchronous battle: a taste of coroutines, await, asyncio

Today, let's briefly talk about those seemingly advanced but mysterious asynchronous programming techniques, commonly known as "coroutines" and "async/await" 

1. What is a coroutine?

Coroutines, also called micro-threads, are a context switching technology in user mode. Simply put, it is a technology that can realize multi-task switching in a single thread. This technology is not provided by the computer, but created by humans.
For example: you are squatting, playing with your mobile phone, smoking while doing multiple tasks at the same time! This is the essence of coroutines.

2. How to use the coroutine?

Let's look at several ways to implement coroutines.

2.1 Using  greenlet the library

This is an early coroutine solution, but it is used less.

from greenlet import greenlet

def func1():
    print(1) # 第二步
    gr2.switch() # 切换到func2
    print(2) # 第四步
	
def func2():
    print(3) # 第三步
    gr1.switch() # 切换到func1
    print(4) # 第五步

gr1 = greenlet(func1)
gr2 = greenlet(func2)

gr1.switch() # 第一步

2.2 Using  yield keywords

yieldIt is a keyword that we are very familiar with for building generators. Yes, this thing can be played with simple coroutines. This method is relatively intuitive and easy to understand.

def func1():
    yield 1
    yield from func2()
    yield 2

def func2():
    yield 3
    yield 4

f1 = func1()
# 别问为啥可以循环哟!
for item in f1:
    print(item)

# 输出 1、3、4、2

2.3 Using  asyncio the library

In Python 3.4 and later, a library can be used  asyncio to implement coroutines.

import asyncio

async def func1():
    print(1)
    # 遇到三上老师的网络 IO 请求
    await asyncio.sleep(2) # 模拟IO耗时操作,开始切换到其他任务
    print(2)

async def func2():
    print(3)
    # 又遇到苍老师的网络IO请求
    await asyncio.sleep(2) # 切换
    print(4)

tasks = [
    asyncio.ensure_future(func1()),
    asyncio.ensure_future(func2())
]

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))

Things to pay attention to in the above code: when encountering IO blocking, it will automatically switch.

2.4 Usage  async and  await keywords

Python 3.5 and later versions introduce  async the and  await keyword, which is more concise and convenient.

import asyncio

async def func1():
    print(1)
  # 又找三上老师的网络 IO 请求
    await asyncio.sleep(2)
    print(2)

async def func2():
    print(3)
   # ...网络IO请求
    await asyncio.sleep(2)
    print(4)

tasks = [
    asyncio.ensure_future(func1()),
    asyncio.ensure_future(func2())
]

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))

Summarizing the above examples, their common steps are:

  1. Define the coroutine function: Note that the call of the coroutine function gets a coroutine object instead of directly executing the function.

  2. Asynchronous operations in coroutine functions

  3. Create event loop ( asyncio ): Use to  asyncio create an event loop object and  asyncio.get_event_loop() get an event loop instance.

  4. Add the coroutine function to the task list: Add the coroutine function to the task list and wait for the event loop to schedule execution.

  5. run event loop

To sum up, these code segments implement coroutines through asynchronous programming, so that multiple tasks can be executed concurrently without blocking. Whether using  greenlet, yield, asyncio or  async/ await, they are all based on the concept of coroutines, and implement the scheduling and execution of asynchronous tasks in different ways

3. Little chestnuts in actual combat: download pictures asynchronously

First, let's use the normal sync method to download images.

import requests

def download_image(url):
    print("开始下载:", url)
    response = requests.get(url)
    print("下载完成")
    file_name = url.rsplit('_')[-1]
    with open(file_name, mode='wb') as f:
        f.write(response.content)

url_list = [
    'https://api.dujin.org/bing/1920.php',
    'https://api.dujin.org/bing/1920.php',
    'https://api.dujin.org/bing/1920.php'
]

for item in url_list:
    download_image(item)

Next, we use asynchronous programming to download multiple images at the same time.

import aiohttp
import asyncio

async def fetch(session, url):
    print("发送请求:", url)
    async with session.get(url, verify_ssl=False) as response:
        content = await response.content.read()
        file_name = url.rsplit('_')[-1]
        with open(file_name, mode='wb') as file_object:
            file_object.write(content)
        print('下载完成', url)

async def main():
    async with aiohttp.ClientSession() as session:
        url_list = [
            'https://api.dujin.org/bing/1920.php',
            'https://api.dujin.org/bing/1920.php',
            'https://api.dujin.org/bing/1920.php'
        ]
        tasks = [asyncio.create_task(fetch(session, url)) for url in url_list]

        await asyncio.wait(tasks)

asyncio.run(main())

4. Asynchronous small chestnut: single interface automated testing.

In interface automation testing, if all test cases have no dependencies, such as in single interface testing, after all parameters are defined independently, you can consider using asynchronous interface requests. This is equivalent to a small pressure on the interface.

import aiohttp
import asyncio

async def test_api(endpoint):
    async with aiohttp.ClientSession() as session:
        async with session.get(endpoint) as response:
            result = await response.json()
            print(f"请求 {endpoint} 返回的结果:{result},断言.......")

async def main():
    # 接口测试用例可以用各种方式读取出来,这里简单搞一搞
    api_endpoints = [
        "http://api.example.com/endpoint1?name=1&age=1",
        "http://api.example.com/endpoint1?name=汉字&age=1",
        "http://api.example.com/endpoint1?name=**&age=1"
        ...
    ]
    tasks = [asyncio.create_task(test_api(endpoint)) for endpoint in api_endpoints]
    await asyncio.wait

asyncio.run(main())

In this way, the interface automation test of asynchronous requests can be easily realized.

Finally: The complete software testing video tutorial below has been sorted out and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

Guess you like

Origin blog.csdn.net/weixin_50829653/article/details/132497247