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的功能远不止于此. 命令就是一 ...
随机推荐
- IntelliJ IDEA 2017.3尚硅谷-----卸载
直接在用户目录下搜索,卸载的干净就要删除 删除这两个目录,重启idea可以还原配置. editplus删除后重启也是这个效果
- python开发基础04-函数、递归、匿名函数、高阶函数、装饰器
匿名函数 lamba lambda x,y,z=1:x+y+z 匿名就是没有名字 def func(x,y,z=1): return x+y+z 匿名 lambda x,y,z=1:x+y+z #与函 ...
- 后台怎么区分接口要写在哪个service类中呢(根据service服务区分)
1,明确页面要实现什么功能,则页面对应的controller要写对应的controller方法 2,这个功能最终要由谁实现完成,在对应的service中药实现这个功能 3,这个接口的实现就写在最终完成 ...
- zxEditor
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="X-UA-C ...
- Ubuntu18.04安装caffe python3.6 opencv3.2 CPU
设置ubuntu的softwares&updates的源为国内源,这样会提高下载速度. 如果是安装python相关库,为提高速度使用: pip3 install 要下载的库 -i https: ...
- 查询数据操作:distinct
1.作用:distinct 去除重复记录.重复记录,指的是字段值,都相同的记录,而不是部分字段值相同的记录 与之相对的是all,表示所有.在MySQL中默认就是all. 2.例子: select ch ...
- Linux源码编译安装nginx
ps:一切从简 一.安装所需环境: yum -y install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openss ...
- python GIL锁与多cpu
多核CPU linux : cat /proc/cpuinfo 如果你不幸拥有一个多核CPU,你肯定在想,多核应该可以同时执行多个线程. 如果写一个死循环的话,会出现什么情况呢? 打开Mac OS ...
- jango 模型管理数据model,数据库外键主键与一对一,一对多,多对多关系
四.models.py 定义和管理模型: 4.1模型class的属性就映射与数据库的字段参数 继承models.Model class TestClass(models.Model): 4.2在数据库 ...
- oracle中以dba_、user_、v$_、all_、session_、index_开头
原 oracle中以dba_.user_.v$_.all_.session_.index_开头 2011年07月05日 11:26:06 clbxp 阅读数:3279 oracle中以dba_.u ...