Study Notes (34): Python Network Programming & concurrent programming - asynchronous calls and callback mechanism

Learning immediately: https://edu.csdn.net/course/play/24458/296452?utm_source=blogtoedu

1. knowledge :( See detailed source notes)

1) synchronous call:

res1 = pool.submit(ju,'john1').result()

2) an asynchronous call

pool.submit(ju,'john1')

3) the difference between the two

Asynchronous call: just submit the task can not wait for the results in place, run the following code immediately
Synchronous call: After you submit your task, still waiting for the results, wait until the results of the implementation of the code behind the code running in a serial effect

4) asynchronous callback mechanism

# Callback mechanism: asynchronous call does not wait for the results, but the results can also be obtained here through the callback mechanism to obtain a concurrent.futures object, you need () to obtain data by .result

5) synchronized with obstruction of difference

Synchronous call whether I / O or compute-intensive need to wait, while blocking occurs only in I / O case

2. synchronous call: synchronous call either I / O or compute-intensive need to wait, while blocking occurs only in I / O case

#同步与异步调用都是提交任务的一种方式,以举重最例

#同步调用:提交完任务后,在原地等待结果,等到结果后再执行后面的代码,代码运行的效果呈串行
from concurrent.futures import ProcessPoolExecutor
import time,random

#创建函数模拟举重的任务
def ju(name):
    print('%s is juing'%name)
    #举重的时间
    time.sleep(random.randint(2,3))
    #举重的重量
    res = random.randint(4,8)
    return {'name':name,'res':res}

#创建函数模拟提交举重结果的事件
def weight(response):
    name = response['name']
    size = response['res']
    print('%s 举重的重量是《%s》kg'%(name,size))

if __name__ == '__main__':
    #创建进程池
    pool = ProcessPoolExecutor(5)
    #提交任务,此处使用同步调用result(),得到函数返回的结果
    res1 = pool.submit(ju,'john1').result()
    weight(res1)
    res2 = pool.submit(ju,'john2').result()
    weight(res2)
    res3 = pool.submit(ju,'john3').result()
    weight(res3)

Synchronous call results

X:\Users\13711\AppData\Local\Programs\Python\Python37\python.exe C:/Users/jinlin/Desktop/python_further_study/并发编程/异步调用与回调机制.py
john1 is juing
john1 举重的重量是《7》kg
john2 is juing
john2 举重的重量是《8》kg
john3 is juing
john3 举重的重量是《7》kg

进程已结束,退出代码0

2. asynchronous call

#异步调用:只是提交了任务即可,不会在原地等待结果,马上运行下面的代码
#回调机制:异步调用不会等待结果,但是也可获得结果,这里通过回调机制,来获得一个concurrent.futures对象,需要通过.result()来获得数据
from concurrent.futures import ProcessPoolExecutor
import time,random

#创建函数模拟举重的任务
def ju(name):
    print('%s is juing'%name)
    #举重的时间
    time.sleep(random.randint(2,3))
    #举重的重量
    res = random.randint(4,8)
    return {'name':name,'res':res}

#创建函数模拟提交举重结果的事件
def weight(response):
    response = response.result()
    name = response['name']
    size = response['res']
    print('%s 举重的重量是《%s》kg'%(name,size))

if __name__ == '__main__':
    #创建进程池
    pool = ProcessPoolExecutor(5)
    #提交任务,此处使用异步调用,不得到函数返回的结果,使用回调函数add_done_callback(回调函数)来得到一个对象
    pool.submit(ju,'john1').add_done_callback(weight)
    pool.submit(ju,'john2').add_done_callback(weight)
    pool.submit(ju,'john3').add_done_callback(weight)

The results of the asynchronous call

X:\Users\13711\AppData\Local\Programs\Python\Python37\python.exe C:/Users/jinlin/Desktop/python_further_study/并发编程/异步调用与回调机制.py
john1 is juing
john2 is juing
john3 is juing
john1 举重的重量是《5》kg
john2 举重的重量是《4》kg
john3 举重的重量是《5》kg

进程已结束,退出代码0

 

Published 49 original articles · won praise 11 · views 565

Guess you like

Origin blog.csdn.net/qq_45769063/article/details/105114489