Use python to help you get up quickly!

Use python to help you get up quickly!

In the python network model, in order to achieve high concurrency, there are many solutions: multi-threading, multi-process. Regardless of multi-threading or multi-process, IO scheduling depends more on the system, and the way of coroutine, scheduling comes from the user

Use coroutines to achieve efficient concurrent tasks. And this operation is called asynchronous IO (asyncio)

Simply put: when we initiate an IO operation without waiting for the end of the instruction set, we can continue to do other things, and when it ends, we will be notified accordingly

Asyncio does not bring true parallelism. Of course, because of the existence of the GIL (Global Interpreter Lock), multithreading using Cython as the Python interpreter (the most common interpreter) cannot bring true parallelism.

The tasks assigned to asyncio are called coroutines. A coroutine can give up execution and give the opportunity to other coroutines (ie yield from or await).

First acquaintance with asyncio

1. let’s get to know Coroutine. I have commented every piece of code.

import asyncio

# asyncio is a standard library introduced in Python 3.4, which directly supports asynchronous IO.
async def coroutine():
    print('in coroutine')

# asyncio's programming model is a message loop
# Get a reference to EventLoop directly from the asyncio module
event_loop = asyncio.get_event_loop()
try:
    print('starting coroutine')
    coro = coroutine()
    print('entering event loop')
    # Throw the coroutine that needs to be executed, here is the coroutine, and throw it into EventLoop for execution
    event_loop.run_until_complete(coro)
finally:
    print('closing event loop')
    event_loop.close()

The result is shown in the figure:

The first is to obtain an event loop asyncio.get_event_loop(), and then use run_until_complete to execute the coroutine object. When the coroutine is executed and exits, run_until_complete will also exit subsequently.

Get Coroutine return value

Just now, we can effectively use the run_until_complete function to execute asyncio. Now we need to do one more step to get the return value of the asynchronous request. run_until_complete will take the return value of Coroutine as its own return value and return it to the caller

import asyncio

async def coroutine():
    print('in coroutine')
    # Added a return value
    return'result'

event_loop = asyncio.get_event_loop()
try:
    # With the previous foundation, we no longer get the object of coroutine separately here
    # run_until_complete will return the return value of coroutine
    return_value = event_loop.run_until_complete(coroutine())
    print(f'it returned: {return_value}')
finally:
    event_loop.close()

The result is shown in the figure:

Chain call

The previous operation is to call a single function, but in work, there are often cases where the function calls the function, let’s take a look

import asyncio

# Function 1
async def one():
    print('in one')
    asyncio.sleep(1)
    print('one end')
    return'one'

# Function 2
async def two(arg):
    print('in two')
    asyncio.sleep(1)
    print('two end')
    return'two with arg {}'.format(arg)

# Will be used as coroutine
async def outer():
    print('in outer')
    print('waiting for one')
    # Wait for the return value of function 1
    result1 = await one()
    print('waiting for two')
    # Wait for the return value of function 2
    result2 = await two(result1)
    # Return 2 results together
    return result1, result2

event_loop = asyncio.get_event_loop()
try:
    return_value = event_loop.run_until_complete(outer())
    print(f'result value: {return_value}')
finally:
    event_loop.close()

Here asyncio.sleep is used instead of time.sleep to simulate asynchronous task requests. Using await can suspend time-consuming operations, just like the yield in the generator, so that the function gives up control. When the coroutine encounters await, the event loop will suspend the coroutine and execute other coroutines until the other coroutines are also suspended or executed, and then proceed to the next coroutine execution to see the results:

Really save a lot of time, this is the power of asynchrony! Well, today’s content ends here, let’s review it together:

  • Python implements asynchronous requests through asyncio
  • Starting from python3.5, the keyword async is used to define coroutine entity functions
  • Use the keyword await to wait for the return value of coroutine

If you are still interested in today's content, why not like it before leaving? If you are interested enough to appreciate me, don’t hesitate~

Reference: https://cloud.tencent.com/developer/article/1153745 Use python to help you get up quickly! -Cloud + Community-Tencent Cloud