python多进程(二)
之前实现的数据共享的方式只有两种结构Value和Array。Python中提供了强大的Manager专门用来做数据共享的,Manager是进程间数据共享的高级接口。 Manager()返回的manager对象控制了一个server进程,此进程包含的python对象可以被其他的进程通过proxies来访问。从而达到多进程间数据通信且安全。Manager支持的类型有list, dict, Namespace, Lock, RLock, Semaphore, BoundedSemaphore, Condition, Event, Queue, Value和Array。
下面看一个用于多进程共享dict和list数据:
from multiprocessing import Manager, Process def worker(dt, lt):
for i in range(10):
dt[i] = i*i # 访问共享数据 lt += [x for x in range(11, 16)] if __name__ == '__main__':
manager = Manager()
dt = manager.dict()
lt = manager.list()
p = Process(target=worker, args=(dt, lt))
p.start()
p.join(timeout=3)
print(dt)
print(lt)
结果:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
[11, 12, 13, 14, 15]
进程池
当使用Process类管理非常多(几十上百个)的进程时,就会显得比较繁琐,Pool可以提供指定数量的进程,供用户调用,当有新的请求提交到pool中时,如果池还没有满,那么就会创建一个新的进程用来执行该请求;但如果池中的进程数已经达到规定最大值,那么该请求就会等待,直到池中有进程结束,才会创建新的进程。
import time import multiprocessing def fun(msg):
print("#########start#### {0}".format(msg))
time.sleep(3)
print("#########end###### {0}".format(msg)) if __name__ == '__main__':
print("start main")
pool = multiprocessing.Pool(processes=3) #在进程池中创建3个进程
for i in range(1, 7):
msg = "hello {0}".format(i)
pool.apply_async(fun, (msg,))# 执行时间6s+
# pool.apply(fun, (msg,)) #执行时间 6*3=18s+
pool.close() #在调用join之前,要先调用close,否则会报错,close执行完不会有新的进程加入到pool
pool.join() #join 是等待所有的子进程结束,必须在close之后调用
print("end main") 结果:
start main
start hello 1
start hello 2
start hello 3
end hello 1
start hello 4
end hello 2
start hello 5
end hello 3
start hello 6
end hello 4
end hello 5
end hello 6
end main
6.18000006676 # 使用pool.apply_async()所花费时间
18.2129998207 # 使用pool.apply()所花费时间
pool.apply_async 非阻塞,定义的进程池最大数的同时执行
pool.apply 一个进程结束,释放回进程池,开始下一个进程
从上面的结果中我们可以看到,使用pool.apply_async所花费的时间较少。
多线程
Python中提供了threading模块来对多线程进行操作,线程是应用程序中工作的最小单元,多线程适用于密集型io。
多线程的实现方式有两种:
方法一:将要执行的方法作为参数传给Thread的构造方法(和多进程类似)
t = threading.Thread(target=action, args=(i,))
方法二:从Thread类继承,并重写run()方法。
方法一示例:
import threading def worker(args):
print("开始子进程 {0}".format(args))
print("结束子进程 {0}".format(args)) if __name__ == '__main__': print("start main")
t1 = threading.Thread(target=worker, args=(1,)) # 和多进程类似
t2 = threading.Thread(target=worker, args=(2,))
t1.start()
t2.start()
print("end main") 结果:
start main
开始子进程 1
结束子进程 1
开始子进程 2
end main
结束子进程 2
方法二示例:
import threading import time class Hello(threading.Thread):
# 对run()方法进行重写
def __init__(self, args):
super(Hello, self).__init__()
self.args = args def run(self):
print("开始子进程 {0}".format(self.args))
time.sleep(1)
print("结束子进程 {0}".format(self.args)) if __name__ == '__main__': a = 1
print("start main")
t1 = Hello(1)
t2 = Hello(2)
t1.start()
t2.start()
print("end main") 结果:
start main
开始子进程 1
开始子进程 2
end main
结束子进程 2
结束子进程 1
线程锁
当多线程争夺锁时,允许第一个获得锁的线程进入临街区,并执行代码。所有之后到达的线程将被阻塞,直到第一个线程执行结束,退出临街区,并释放锁。
需要注意,那些阻塞的线程是没有顺序的。
import time
import threading def sub():
global count
lock.acquire() #上锁,第一个线程如果申请到锁,会在执行公共数据的过程中持续阻塞后续线程
#即后续第二个或其他线程依次来了发现已经被上锁,只能等待第一个线程释放锁
#当第一个线程将锁释放,后续的线程会进行争抢 '''线程的公共数据'''
temp=count
print("temp is {0}".format(temp))
time.sleep(0.001)
count=temp+1
print("count is {0}".format(count))
'''线程的公共数据''' lock.release() # 释放锁
time.sleep(2)
count=0 l=[]
lock=threading.Lock()
for i in range(100):
t=threading.Thread(target=sub,args=())
t.start()
l.append(t)
for t in l:
t.join()
print(count) 结果:
100
线程池
采用线程池,花费的时间更少。
语法结构示例:
import threadpool
def ThreadFun(arg1,arg2):
pass
def main():
device_list=[object1,object2,object3......,objectn]#需要处理的设备个数
task_pool=threadpool.ThreadPool(8)#8是线程池中线程的个数
request_list=[]#存放任务列表
#首先构造任务列表
for device in device_list:
request_list.append(threadpool.makeRequests(ThreadFun,[((device, ), {})]))
#将每个任务放到线程池中,等待线程池中线程各自读取任务,然后进行处理,使用了map函数,不了解的可以去了解一下。
map(task_pool.putRequest,request_list)
#等待所有任务处理完成,则返回,如果没有处理完,则一直阻塞
task_pool.poll()
if __name__=="__main__":
main()
pip install threadpool # 安装threadpool
from threadpool import * #导入模块
pool = ThreadPool(size) # 定义一个线程池,最多能创建size个线程
requests = makeRequests() # 调用makeRequests创建了要开启多线程的函数,以及函数相关参数和回调函数,其中回调函数可以不写,default是无,也就是说makeRequests只需要2个参数就可以运行;
[pool.putRequest(req) for req in requests] # 将所有要运行多线程的请求扔进线程池
pool.wait() # 等待所有的线程完成工作后退出。
[pool.putRequest(req) for req in requests]等同于:
for req in requests:
pool.putRequest(req)
例子:
import threadpool def hello(m, n, o):
print("m = {0} n={1} o={2}".format(m, n, o)) if __name__ == '__main__':
# 方法1
lst_vars_1 = ['', '', ''] # 需要处理的线程个数
lst_vars_2 = ['', '', '']
func_var = [(lst_vars_1, None), (lst_vars_2, None)]
# 方法2
# dict_vars_1 = {'m': '1', 'n': '2', 'o': '3'}
# dict_vars_2 = {'m': '4', 'n': '5', 'o': '6'}
# func_var = [(None, dict_vars_1), (None, dict_vars_2)] pool = threadpool.ThreadPool(2) # 线程池中的线程个数
requests = threadpool.makeRequests(hello, func_var) # 创建了要开启多线程的函数,创建需要线程池处理的任务,只需要两个参数
[pool.putRequest(req) for req in requests] # 将每个任务放入到线程池中
pool.wait() 结果:
m = 1 n=2 o=3
m = 4 n=5 o=6
python多进程(二)的更多相关文章
- 【Python从入门到精通】(二十五)Python多进程的使用
您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 本篇重点介绍Python多进程的使用,读者朋友们可以将多进程和多线程两者做一个对比学习. 干货满满,建议收藏,需要用到时常看看. 小伙伴们如有问题 ...
- Python多进程(1)——subprocess与Popen()
Python多进程方面涉及的模块主要包括: subprocess:可以在当前程序中执行其他程序或命令: mmap:提供一种基于内存的进程间通信机制: multiprocessing:提供支持多处理器技 ...
- Python 多进程multiprocessing
一.python多线程其实在底层来说只是单线程,因此python多线程也称为假线程,之所以用多线程的意义是因为线程不停的切换这样比串行还是要快很多.python多线程中只要涉及到io或者sleep就会 ...
- Python 多进程 多线程 协程 I/O多路复用
引言 在学习Python多进程.多线程之前,先脑补一下如下场景: 说有这么一道题:小红烧水需要10分钟,拖地需要5分钟,洗菜需要5分钟,如果一样一样去干,就是简单的加法,全部做完,需要20分钟:但是, ...
- 【转】【Python】Python多进程与多线程
1.1 multiprocessing multiprocessing是多进程模块,多进程提供了任务并发性,能充分利用多核处理器.避免了GIL(全局解释锁)对资源的影响. 有以下常用类: 类 描述 P ...
- python多进程详解
目录 python多进程 序.multiprocessing 一.Process process介绍 例1.1:创建函数并将其作为单个进程 例1.2:创建函数并将其作为多个进程 例1.3:将进程定义为 ...
- 一篇文章搞定Python多进程(全)
1.Python多进程模块 Python中的多进程是通过multiprocessing包来实现的,和多线程的threading.Thread差不多,它可以利用multiprocessing.Proce ...
- python多进程multiprocessing Pool相关问题
python多进程想必大部分人都用到过,可以充分利用多核CPU让代码效率更高效. 我们看看multiprocessing.pool.Pool.map的官方用法 map(func, iterable[, ...
- python 多进程数量 对爬虫程序的影响
1. 首先看一下 python 多进程的优点和缺点 多进程优点: 1.稳定性好: 多进程的优点是稳定性好,一个子进程崩溃了,不会影响主进程以及其余进程.基于这个特性,常常会用多进程来实现守护服务器的功 ...
- Python多进程编程
转自:Python多进程编程 阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiproces ...
随机推荐
- KATANA Owin 资料收集
https://www.cnblogs.com/xishuai/p/asp-net-5-owin-katana.html http://wiki.jikexueyuan.com/project/thi ...
- BootStrap-select 插件的使用
这是一款下拉框多选的插件,非常的抢到,什么样式都是有的:首先去参看一下官网的信息,详细介绍是怎么使用的: 相关官网网址: https://silviomoreto.github.io/bootstr ...
- C#简单工厂模式(学习Learning hard讲解笔记)
原味地址http://www.cnblogs.com/zhili/p/SimpleFactory.html 简单工厂模式通俗的理解就是用户与工厂的关系,用户用的东西,工厂来生成,责任明确. 就像大神展 ...
- CSS3 @font-face实现颜色大小可控的三角效果——张鑫旭
一.我之前介绍过的三角实现效果回顾 这里所说的三角效果之等腰直角三角形效果(等边三角形有现成字符实现,没什么好说的:还有图片实现三角众人皆知,不予以说明): 1. 字符实现三角效果关于字符实现三角我早 ...
- 基于token的后台身份验证(转载)
几种常用的认证机制 HTTP Basic Auth HTTP Basic Auth简单点说明就是每次请求API时都提供用户的username和password,简言之,Basic Auth是配合RES ...
- 黑客之google入侵网站常用方式 2
一: 在搜索框上输入: “index of/ ” inurl:lib 再按搜索你将进入许多图书馆,并且一定能下载自己喜欢的书籍. 在搜索框上输入: “index of /” cnki 再按搜索你就可以 ...
- ArcGIS10.3+Oracle12C+ArcGIS Server10.3安装布署(之三)
1.将Oracle的客户端切换到64位 (1)将C:\下的instantclient_12_1目录重命名为instantclient_12_1X86 (2)从Oracle的官方网站下载 insta ...
- Vue 框架-07-循环指令 v-for,和模板的使用
Vue 框架-07-循环指令 v-for,和模板的使用 本章主要是写一些小实例,记录代码,想要更详细的话,请查看 官方文档:https://cn.vuejs.org/v2/guide/#%E6%9D% ...
- 树莓派发射FM波——搭建私人小电台
树莓派的应用十分广泛,有很多奇思妙想的应用非常有趣,在这里我们想实现一个小电台的功能,但是在这里需要说明,私人架设电台是违法行为,所以本案只作为自我娱乐所用,不能发射大功率的信号干扰正常的FM频段. ...
- Django 模板继承extend 标签include block
# block 站网页位置# includ 导入网页标签# extends 导入网页模板 # common_js.html <script src="/static/plugins/j ...