python 之并发编程更新版进程池与进程池比较与回调函数
一.更新版进程池与进程池比较
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
import os, time def func(i):
print('Process', i, os.getpid())
time.sleep(0.1)
print("Process..end")
return 88899
# (1)ProcessPoolExcutor 进程池的基本使用(改良版)
相对于旧版的进程池,
一定会等待子进程全部执行完毕之后,再终止程序,相当于过去的Process流程
shutdown 相当于Process里面的join
if __name__ == "__main__":
# (1)ProcessPoolExecutor() <==> Pool()
p = ProcessPoolExecutor(5)
# (2)submit() <==> apply_async()
res = p.submit(func, 55)
# (3)result() <==> get()
res = res.result()
print(res) #
# (4)shutdown <==> close + join
#p.shutdown()
print("主进程执行结束...")
# (2)线程池
from threading import current_thread as ct
def func(i):
print("thread",i,ct().ident)
time.sleep(0.1)
print("thread %s end" % (i)) #可以在参数中指定并发的线程数
tp = ThreadPoolExecutor(10)
for i in range(20):
tp.submit(func,i)
#tp.shutdown()
print("主线程执行结束...")
# (3)线程池的返回值
from threading import current_thread as cthread def func(i):
print("thread", i, cthread().ident)
# 加延迟防止个别线程因为执行速度过快,又接收任务,阻碍新线程的创建
# time.sleep(0.1)
print("threading %s end" % (i))
# return "*" * i
return cthread().ident tp = ThreadPoolExecutor()
lst = []
setvar = set()
for i in range(10):
res = tp.submit(func,i)
lst.append(res) for i in lst:
# print(i.result())
setvar.add(i.result())
print(setvar,len(setvar))
print("主线程执行结束...")
# (4)map 返回迭代器
from threading import current_thread as cthread
def func(i):
print("threading",i,cthread().ident)
time.sleep(0.1)
print("thread %s end" % (i))
return "*" * i tp = ThreadPoolExecutor(5)
it = tp.map(func,range(20)) # map
from collections import Iterable,Iterator
print(isinstance(it,Iterator))
for i in it:
print(i) tp.shutdown()
print("主线程执行结束..")
二.回调函数
回调函数:
把函数当成参数传递的另外一个函数
函数先执行,最后在执行当参数传递的这个函数,整个过程是回调,这个参数是回调函数
# (1) 线程池的回调函数是由 子线程完成
from concurrent.futures import ThreadPoolExecutor
from threading import current_thread as cthread import time
def func(i):
print("thread",i,cthread().ident)
time.sleep(0.1)
print("thread %s end" % (i))
return "*" * i # 定义成回调函数
def call_back(args):
print("call back:",cthread().ident)
print(args.result()) tp = ThreadPoolExecutor(5)
for i in range(1,11):
# submit(函数,参数).add_done_callback(要添加的回调函数)
tp.submit(func,i).add_done_callback(call_back) tp.shutdown()
print("主线程:",cthread().ident)
# (2) 进程池的回调函数是由 主进程完成
from concurrent.futures import ProcessPoolExecutor
import os,time
def func(i):
print("Process",i,os.getpid())
time.sleep(0.1)
print("Process %s end" % (i)) if __name__ == "__main__":
p = ProcessPoolExecutor(5)
p.submit(func,11)
p.shutdown()
print("主进程:",os.getpid())
例2:
from concurrent.futures import ProcessPoolExecutor
import os,time
def func(i):
print("Process",i,os.getpid())
time.sleep(0.1)
print("Process %s end" % (i))
return i * "*" # 回调函数
def call_back(args):
print("call back:",os.getpid())
# print(args)
print(args.result()) if __name__ == "__main__":
# 同一时间最多允许5个进程并发
tp = ProcessPoolExecutor(5)
for i in range(1,11):
tp.submit(func,i).add_done_callback(call_back)
tp.shutdown()
print("主进程id:",os.getpid())
python 之并发编程更新版进程池与进程池比较与回调函数的更多相关文章
- Python并发编程06 /阻塞、异步调用/同步调用、异步回调函数、线程queue、事件event、协程
Python并发编程06 /阻塞.异步调用/同步调用.异步回调函数.线程queue.事件event.协程 目录 Python并发编程06 /阻塞.异步调用/同步调用.异步回调函数.线程queue.事件 ...
- python 之 并发编程(守护线程与守护进程的区别、线程互斥锁、死锁现象与递归锁、信号量、GIL全局解释器锁)
9.94 守护线程与守护进程的区别 1.对主进程来说,运行完毕指的是主进程代码运行完毕2.对主线程来说,运行完毕指的是主线程所在的进程内所有非守护线程统统运行完毕,主线程才算运行完毕详细解释:1.主 ...
- Python 3 并发编程多进程之守护进程
Python 3 并发编程多进程之守护进程 主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就终止 其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemo ...
- Python 3 并发编程多进程之进程同步(锁)
Python 3 并发编程多进程之进程同步(锁) 进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的,竞争带来的结果就是错乱,如何控制,就是加锁处理. 1. ...
- Python 3 并发编程多进程之队列(推荐使用)
Python 3 并发编程多进程之队列(推荐使用) 进程彼此之间互相隔离,要实现进程间通信(IPC),multiprocessing模块支持两种形式:队列和管道,这两种方式都是使用消息传递的. 可以往 ...
- 并发编程学习笔记(14)----ThreadPoolExecutor(线程池)的使用及原理
1. 概述 1.1 什么是线程池 与jdbc连接池类似,在创建线程池或销毁线程时,会消耗大量的系统资源,因此在java中提出了线程池的概念,预先创建好固定数量的线程,当有任务需要线程去执行时,不用再去 ...
- Python 3 并发编程多进程之进程池与回调函数
Python 3 进程池与回调函数 一.进程池 在利用Python进行系统管理的时候,特别是同时操作多个文件目录,或者远程控制多台主机,并行操作可以节约大量的时间.多进程是实现并发的手段之一,需要注意 ...
- python并发编程之多进程2-(数据共享及进程池和回调函数)
一.数据共享 1.进程间的通信应该尽量避免共享数据的方式 2.进程间的数据是独立的,可以借助队列或管道实现通信,二者都是基于消息传递的. 虽然进程间数据独立,但可以用过Manager实现数据共享,事实 ...
- python并发编程之多进程2数据共享及进程池和回调函数
一.数据共享 尽量避免共享数据的方式 可以借助队列或管道实现通信,二者都是基于消息传递的. 虽然进程间数据独立,但可以用过Manager实现数据共享,事实上Manager的功能远不止于此. 命令就是一 ...
随机推荐
- bugku 求getshell
要修改三个地方 根据大佬们的writeup,要修改三个地方: 1.扩展名filename 2.filename下面一行的Content-Type:image/jpeg 3.最最最重要的是请求头里的Co ...
- bugku 散乱密码
BugkuCTF_加密_散乱的密文 WriteUp image.png lf5{ag024c483549d7fd@@1} 一张纸条上凌乱的写着2 1 6 5 3 4 以前做过这种类型的 既然是凌乱 ...
- 吴裕雄 python 机器学习——数据预处理过滤式特征选取VarianceThreshold模型
from sklearn.feature_selection import VarianceThreshold #数据预处理过滤式特征选取VarianceThreshold模型 def test_Va ...
- 读写json文件
def read_json(path): """return dict""" with open(path,'r+')as f: retur ...
- Dart语言学习(七)Dart Map类型
映射(Maps)是无序的键值对: 常用属性: keys 获取所有的key值 values 获取所有的value值 isEmpty 是否为空 isNotEmpty 是否不为空 常用方法: remove( ...
- Appium-测试失败后获取屏幕截图的方法
最近一直在研究appium,偶尔的机会发现断言后获取屏幕截图.觉得这个方法不错,分享给大家 这样以后在遇到断言,想截图错误屏幕的时候,能够用的上. 1.首先需要2个类,一个是测试类(TestDropL ...
- Centos7编译安装kafka-manager-2.0.0.2
一.kafka-manager简介 项目地址为:https://github.com/yahoo/kafka-manager 为了简化开发者和服务工程师维护Kafka集群的工作,yahoo构建了一个叫 ...
- N个数求和(PTA)
这题多输出了一个空格,卡了半天... leetcode刷多了,后遗症 这题可以用scanf("%lld/%lld"),直接读入,不过我用了stoll,也就是stoi,string ...
- oracle 基础sql语句
修改date日期时间: update T2_FOODS_STORAGE_IN set create_time =to_date('2020-01-15 12:30:20','yyyy-mm-dd hh ...
- layui-表格宽度自适应
不设置表格宽度,表格默认全屏 可以通过以下方式设置表格宽度