One 、 Asynchronous programming overview

Asynchronous programming is a mode of concurrent programming , Its focus is to schedule the execution and waiting time between different tasks , Reduce the execution time of the whole program by reducing the idle time of the processor ; The biggest difference between asynchronous programming and synchronous programming model is its task switching , When you encounter a task that needs to wait for a long time , We can switch to other tasks ;

Compared with multithreading and multiprocessing programming models , Asynchronous programming is just task scheduling within the same thread , Can't make full use of multi-core CPU The advantages of , So it is especially suitable for IO Blocking tasks ;

python edition 3.9.5

Two 、python Asynchronous framework model

python Provides asyncio Module to support asynchronous programming , It involves coroutines、event loops、futures Three important concepts ;

event loops Mainly responsible for tracking and scheduling all asynchronous tasks , Choreograph specific tasks to be performed at a certain point in time ;

coroutines It is the encapsulation of specific tasks , Is one that can pause during execution and switch to event loops A special type of function that executes a process ; It generally needs to create task Can be event loops Dispatch ;

futures Responsible for bearing coroutines The results of the implementation of , It follows the task in event loops Created by initialization in , And record the execution status of the task with the execution of the task ;

The whole execution process of asynchronous programming framework involves the close cooperation of the three ;

First event loops After starting , The first task to be executed will be obtained from the task queue coroutine, And then create the corresponding task and future;

Then as the task Implementation , When you meet coroutine Where internal tasks need to be switched ,task The execution of will pause and release the execution thread to event loop,event loop Then the next to be executed... Will be obtained coroutine, After relevant initialization , Execute this task;

With event loop Finish executing the last... In the queue coroutine Will switch to the first coroutine;

With task End of execution for ,event loops Will task Clear out the queue , The corresponding execution results will be synchronized to future in , This process will continue until all task end of execution ;

3、 ... and 、 Execute multiple overlapping tasks in sequence

Each task will be suspended for a given time , The sequential execution time is the sum of the execution time of each task ;

import time

def count_down(name, delay):
indents = (ord(name) - ord('A')) * '\t' n = 3
while n:
time.sleep(delay)
duration = time.perf_counter() - start
print('-' * 40)
print(f'{duration:.4f} \t{indents}{name} = {n}')
n -= 1 start = time.perf_counter() count_down('A', 1)
count_down('B', 0.8)
count_down('C', 0.5)
print('-' * 40)
print('Done') # ----------------------------------------
# 1.0010 A = 3
# ----------------------------------------
# 2.0019 A = 2
# ----------------------------------------
# 3.0030 A = 1
# ----------------------------------------
# 3.8040 B = 3
# ----------------------------------------
# 4.6050 B = 2
# ----------------------------------------
# 5.4059 B = 1
# ----------------------------------------
# 5.9065 C = 3
# ----------------------------------------
# 6.4072 C = 2
# ----------------------------------------
# 6.9078 C = 1
# ----------------------------------------
# Done

Four 、 Asynchronous synchronous code

python Grammatically provides async、await Two keywords to simplify changing synchronous code to asynchronous code ;

async Used in the function def Keyword front , Mark this is a coroutine function ;

await Use in conroutine inside , Used to mark that the execution process needs to be suspended and released event loops;

await The following expression needs to return waitable The object of , for example conroutine、task、future etc. ;

asyncio The module mainly provides operation event loop The way ;

We can go through async take count_down Marked as coroutine, And then use await and asyncio.sleep To achieve asynchronous pause , So as to give control to event loop;

async def count_down(name, delay, start):
indents = (ord(name) - ord('A')) * '\t' n = 3
while n:
await asyncio.sleep(delay)
duration = time.perf_counter() - start
print('-' * 40)
print(f'{duration:.4f} \t{indents}{name} = {n}')
n -= 1

Our definition of asynchrony main Method , Mainly complete task Create and wait for the completion of task execution ;

async def main():
start = time.perf_counter()
tasks = [asyncio.create_task(count_down(name,delay,start)) for name, delay in [('A', 1),('B', 0.8),('C', 0.5)]]
await asyncio.wait(tasks)
print('-' * 40)
print('Done')

We can see that the time has become the time to perform the longest task ;

asyncio.run(main())

# ----------------------------------------
# 0.5010 C = 3
# ----------------------------------------
# 0.8016 B = 3
# ----------------------------------------
# 1.0011 A = 3
# ----------------------------------------
# 1.0013 C = 2
# ----------------------------------------
# 1.5021 C = 1
# ----------------------------------------
# 1.6026 B = 2
# ----------------------------------------
# 2.0025 A = 2
# ----------------------------------------
# 2.4042 B = 1
# ----------------------------------------
# 3.0038 A = 1
# ----------------------------------------
# Done

5、 ... and 、 Overcome the specific limitations of asynchronous multithreading

Asynchronous programming requires that the specific task must be coroutine, That is, the method is required to be asynchronous , Otherwise, only the task is completed , To release control to event loop;

python Medium concurent.futures Provides ThreadPoolExecutor and ProcessPoolExecutor, It can be used directly in asynchronous programming , Thus, tasks can be performed in a separate thread or process ;

import time
import asyncio
from concurrent.futures import ThreadPoolExecutor def count_down(name, delay, start):
indents = (ord(name) - ord('A')) * '\t' n = 3
while n:
time.sleep(delay) duration = time.perf_counter() - start
print('-'*40)
print(f'{duration:.4f} \t{indents}{name} = {n}')
n -=1 async def main():
start = time.perf_counter()
loop = asyncio.get_running_loop()
executor = ThreadPoolExecutor(max_workers=3)
fs = [
loop.run_in_executor(executor, count_down, *args) for args in [('A', 1, start), ('B', 0.8, start), ('C', 0.5, start)]
] await asyncio.wait(fs)
print('-'*40)
print('Done.') asyncio.run(main()) # ----------------------------------------
# 0.5087 C = 3
# ----------------------------------------
# 0.8196 B = 3
# ----------------------------------------
# 1.0073 A = 3
# ----------------------------------------
# 1.0234 C = 2
# ----------------------------------------
# 1.5350 C = 1
# ----------------------------------------
# 1.6303 B = 2
# ----------------------------------------
# 2.0193 A = 2
# ----------------------------------------
# 2.4406 B = 1
# ----------------------------------------
# 3.0210 A = 1
# ----------------------------------------
# Done.

python More articles on asynchronous programming

  1. Python Asynchronous programming [0] -> coroutines [0] -> Coroutines and async / await

    coroutines / Coroutine Catalog Producer consumer model From generators to asynchronous coroutines – async/await A coroutine can be interrupted at a predetermined or random position of a subroutine during the execution of a thread , Then move on to other subroutines , At the right time ...

  2. Python Asynchronous programming [0] -> coroutines [1] -> Build your own asynchronous non blocking model using coroutines

    Build your own asynchronous non blocking model using coroutines In the next example , Will use pure Python Code to build an asynchronous model , It's equivalent to a self built one asyncio modular , It might be good for asyncio Understanding the underlying implementation of the module is more helpful . The main reference is ...

  3. In depth understanding of Python Asynchronous programming ( On )

    http://python.jobbole.com/88291/ Preface Many of my friends are in a dilemma about asynchronous programming " I hear it's powerful " The cognitive state of . It's rarely used in production projects . And the students who use it , Most of them stay in knowledge ...

  4. python Asynchronous programming

    Python 3.5 What is Xiecheng Yushneng · Mar 10th, 2016 Link to the original text : How the heck does async/await work in Python 3.5 ...

  5. use Python 3 Of async / await Do asynchronous programming

    I wrote an article the year before last < On Python 3 The asynchronous IO Programming >, At that time, I just had a preliminary contact  yield from  Grammar and  asyncio  Standard library . I was in... The other day V2EX See an article < Why? ...

  6. Quickly understand Python The basic principle of asynchronous programming

    First example Suppose you need to cook in an electric cooker , Wash clothes in the washing machine , Call a friend and ask him to come over for dinner . among , Rice cookers need 30 It takes minutes to cook the meal , The washing machine needs 40 It'll take ten minutes to wash the clothes , Friends need 50 Minutes to your house . that , Do you need to be here ...

  7. Python Network programming (4)—— Asynchronous programming select &amp; epoll

    stay SocketServer Module learning , We learned about multithreading and multiprocessing simply Server The implementation of the , Using multithreading . The server side of multiprocessing technology is for every new client Connect to create a new entry / Threads , When client When there are more , This technique ...

  8. utilize python yielding Creating a coroutine synchronizes asynchronous programming

    from :http://www.jackyshen.com/2015/05/21/async-operations-in-form-of-sync-programming-with-python-yiel ...

  9. Atitit. Asynchronous programming java .net php python js contrast

    Atitit. Asynchronous programming  java .net php python js  The comparison of 1. 1. Asynchronous task , Asynchronous mode ,  APM Pattern ,,  EAP Pattern , TAP 1 1.1.       APM Pattern : Beg ...

  10. ( turn )python Asynchronous programming -- Callback model (selectors modular )

    original text :https://www.cnblogs.com/zzzlw/p/9384308.html#top Catalog 0. Reference address 1. Preface 2. Core class 3. SelectSelector The core function code is divided into ...

Random recommendation

  1. Artificial intelligence AI- Machine vision CV- data mining DM- machine learning ML- neural network -[ Information collection post ]

    explain : This post is used to collect information that the author can collect and find useful , Convenient for all parties to refer to , Free from the search , Enhance the cross reference value of information . For reference only , Not as the inevitable tendency to recommend . If it involves copyright and other issues, please contact the author , thank you . | Blog | ...

  2. KNN Recognize the number on the image and python Realization

    The leader asked me to input it manually every day BI Data in the system and judge whether the data is abnormal , If there's something unusual , Then detect whether it is a system problem or a business problem . In order to free hands , I decided to write a program to complete the task of entering and managing cockpit data every day . First of all, we recorded a set of scripts with the key wizard to put the ...

  3. Absolutely dazzling 3D Slide -SLICEBOX

    Absolutely dazzling 3D Slide -SLICEBOX http://www.jq22.com/jquery-info31

  4. Incisive JQuery( Four )

    Forms : A form has three basic components : Form labels . Form field . Form button Cookie: stay jQuery One of them is Cookie plug-in unit ,<script src="js/jquery.cookie.js ...

  5. One 、 What is? WPF?

    One . What is? WPF? Windows Presentation Foundation( The previous code name was “Avalon”) yes Microsoft be used for Windows Unified display subsystem , It passes through WinFX Male ...

  6. ubuntu uninstall qq2012

    [email protected]:~$ sudo dpkg --purge wine-qq2012-longeneteam [sudo] password for xianb ...

  7. 【BZOJ2154】Crash The number table of ( Mobius inversion )

    [BZOJ2154]Crash The number table of ( Mobius inversion ) Topic BZOJ Simplify the question : Given \(n,m\) seek \[\sum_{i=1}^n\sum_{j=1}^mlcm(i,j)\] Answer key All of the following ...

  8. Super simple jQuery Front page Pagination , There is no need to guide the package

    Today, we introduce a method that does not need to import paging package , Very easy to use pagination + Fuzzy query function . Next, we will introduce the paging function : First step , You need to have a list to paginate . I have a simple library management here , As the basis of the exhibition , Its list is submitted asynchronously , By two parts ...

  9. Unity Access the surveillance camera

    Public network RTSP Address of the test : rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov RTSP Testing software : EasyPlayerRTSP: https:// ...

  10. Remote access MySQL

    Honing technology, Zhuji , Practice the way of data , Pursuit of outstanding value   Back to the previous page : PostgreSQL Miscellany page       Back to the top page :PostgreSQL Index page  [ author Gao Jian @ Blog Garden  [email protected] ...