学习日记0910线程池与进程池 同步调用与异步调用 函数回调

2021-07-03 20:04

阅读:800

标签:之间   操作   res   http   阻塞   span   main   同步   proc   

前提:线程池与进程池一般与同步调用与异步调用一起使用,而函数回调一般与异步调用一起使用

同步调用

  提交完一个任务后,在原地等待,直到任务完完整整的运行完,得到返回的结果之后,再运行下一个函数,这样会导致任务之间是串行运行

  进程池+同步调用

from concurrent.futures import ProcessPoolExecutor
import time,os,random

def task(name):
    print(%s:%s % (name, os.getpid()))
    time.sleep((random.randint(1, 3)))
    return 123


if __name__ == __main__:
    p = ProcessPoolExecutor(4)
    for i in range(10):
        res = p.submit(task,同步调用).result()
        print(res)
    p.shutdown(wait=True)

    print(主函数)

  进程池+异步调用+函数回调

from concurrent.futures import ProcessPoolExecutor
import time
import random, os


def task(name):
    print(%s:%s % (name, os.getpid()))
    time.sleep((random.randint(1, 3)))
    return 123


def re_task(future):
    time.sleep(1)
    print(函数task的运行结果是%s:%s % (future.result(), os.getpid()))


if __name__ == __main__:
    p = ProcessPoolExecutor(4)
    for i in range(10):
        future = p.submit(task, pid)
        future.add_done_callback(re_task)  # 函数回调的代码,实现的是将future对象传递给re_task函数,实现了函数之间的解耦合
    p.shutdown(wait=True)

    print(zhu)

  线程池+同步调用

from concurrent.futures import ThreadPoolExecutor
from threading import current_thread
import time,random

def task(name):
    print(%s:%s % (name, current_thread().name))
    time.sleep((random.randint(1, 3)))
    return 123

if __name__ == __main__:
    t = ThreadPoolExecutor(4)
    for i in range(10):
        res = t.submit(task,pid).result()
        print(res)
    t.shutdown(wait=True)
    print(zhu)

  线程池+异步调用+函数回调

from concurrent.futures import ThreadPoolExecutor
from threading import current_thread
import time,random

def task(name):
    print(%s:%s % (name, current_thread().name))
    time.sleep((random.randint(1, 3)))
    return 123

def re_task(future):
    time.sleep(1)
    print(函数task的运行结果是%s:%s % (future.result(), current_thread().name))


if __name__ == __main__:
    t = ThreadPoolExecutor(4)
    for i in range(10):
        future = t.submit(task,pid)
        future.add_done_callback(re_task)
    t.shutdown(wait=True)
    print(主线程!)

阻塞与非阻塞

  阻塞:程序正在处理I\O操作程序会进入阻塞状态

  非阻塞:程序正在处在运行状或者就绪态

程序的三种状态:

技术分享图片

 

学习日记0910线程池与进程池 同步调用与异步调用 函数回调

标签:之间   操作   res   http   阻塞   span   main   同步   proc   

原文地址:https://www.cnblogs.com/jianhaozhou/p/9618518.html


评论


亲,登录后才可以留言!