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的功能远不止于此. 命令就是一 ...
随机推荐
- extern "C" 与函数重载
前言 如果向要在一个文件中使用另一个文件中的变量,不能在头文件中定义全局变量,因为被多个文件包含后会导致编译出错,并且静态的static变量,只能在本文件内使用,这时候就可以使用extern关键字. ...
- gVim for windows 简单使用教程
vim 是一个具有很多命令的功能非常强大的编辑器.限于篇幅,在本教程当中 就不详细介绍了.本教程的设计目标是讲述一些必要的基本命令,而掌握好这 些命令,您就能够很容易将vim当作一个通用的 ...
- 闲来无事.gif
- 计算几何-UVa10652
This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 题意见白书,P2 ...
- AS报错:Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict with dependency 'com.and
build->Rebuid-project 寻找错误根源: 报错里可以发现: Resolved versions for app (26.1.0) and test app (27.1.1) d ...
- 类型type:clusterip和service内部的关系
类型type:clusterip和service内部的关系 待办 https://stackoverflow.com/questions/41509439/whats-the-difference-b ...
- Django流程-以登录功能为例
Django流程-以登录功能为例 一.注意点 1.新创建的app一定要先去settings.py注册 简写:'app01' 完整:'app01.apps.App01Config' 2.启动Django ...
- 【Python collections】
目录 namedtuple deque Counter OrderedDict defaultdict "在内置数据类型(dict.list.set.tuple)的基础上,collectio ...
- stream.js
<script src='stream-min.js'></script> 下载 stream.js 2Kb minified streams是什么? Streams 是一个操 ...
- tp5 自定义公共函数,前台模板调用
最近用tp5做一个cms,在添加模型的时候,选择类型,这类型太多了,如果一个个的去判断显示,能累死人了,干脆写个公共方法, 首先写公共方法用到Common.php,目录project/applicat ...