多进程multiprocess模块

multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.

这个模块的语法结构与threading模块基本相似。

查找到一个多进程的博客连接:https://www.cnblogs.com/Mr-Murray/p/9026955.html

from  multiprocessing import Process
import time
import os def info():
print("\033[35;1mThe time is %s\033[0m" % time.ctime())
print("The parent is %s; The Child process is %s" % (os.getppid(), os.getpid())) if __name__ == "__main__":
for i in range(10):
p = Process(target=info)
p.start()
p.join()

这段代码和之前用threading模块创建的多线程并没有任何的区别,但是如果在windows系统上执行时,必须加上if __name__ == "__main__":语句,原因如下:

在Windows操作系统中由于没有fork(linux操作系统中创建进程的机制),在创建子进程的时候会自动 import 启动它的这个文件,
而在 import 的时候又执行了整个文件。因此如果将process()直接写在文件中就会无限递归创建子进程报错。所以必须把创建子
进程的部分使用if __name__ ==‘__main__’ 判断保护起来,import 的时候 ,就不会递归运行了。 以实例化的方式产生进程:
import os, time
from multiprocessing import Process class Myprocess(Process):
def __init__(self):
super(Myprocess, self).__init__() def run(self):
print("\033[35;1mThe time is %s\033[0m" % time.ctime())
print("The parent is %s; The Child process is %s" % (os.getppid(), os.getpid())) if __name__ == "__main__":
p = Myprocess()
p.start()
#在调用p.start的时候,会自动执行类中的run方法,run方法是必不可少的。和threading类中的Thread中的run方法一样。

进程中的守护进程和threading模块中的守护线程是同一个意思,主进程结束的时候,子进程也会随之结束。

import os, time
from multiprocessing import Process def fun1():
print("starting %s".center(50,"-") % time.ctime())
time.sleep(3)
print("Stopping %s".center(50,"-") % time.ctime()) if __name__ == "__main__":
p = Process(target=fun1)
p.daemon = True
p.start()
time.sleep(1) --------------------------------------------------------------------------
子进程中是要执行3s的,但主进程中只执行了1s,设置了守护进程之后,主进程结束,不管子进程的状态,子进程都要退出

利用多进程实现简易的socket并发连接。

server端代码:
import socket, time
from multiprocessing import Process def handle(conn,addr):
print("The connection is from %s at %s port" % addr)
conn.send(b"Hello world")
data = conn.recv(1024)
conn.send(data) if __name__ == "__main__":
HOST = "localhost"
PORT = 51423
ADDR = (HOST, PORT) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(ADDR)
s.listen(5) while True:
conn, addr = s.accept()
p = Process(target=handle, args=(conn,addr)) # 进程间的数据是不共享的,因此需要把coon作为参数传递
p.start()
s.close() client端代码:
import socket HOST = "localhost"
PORT = 51423
ADDR = (HOST, PORT) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(ADDR)
data = s.recv(1024)
print(data.decode())
while True:
info = input(">>>: ")
s.send(info.encode())
data = s.recv(1024)
print(data.decode())

注意:

进程之间的数据是彼此之间不能互相访问的,因此conn必须作为参数传递。

进程之间的通信该怎么做?

进程中的队列和管道:

from multiprocessing import Process, Queue
from time import sleep
import random # 利用进程写一个生成消费者模型,
# 生成者向队列插入一个随机数,消费者取出队列中的一个数值 def productor(q):
while True:
sleep(0.3)
print("store an num")
q.put(random.randint(1,100)) def consumer(q):
while True:
sleep(0.2)
print("Getting an num")
q.get() if __name__ == "__main__":
q = Queue()
proc1 = Process(target=productor, args=(q,))
proc2 = Process(target=consumer, args=(q,))
proc1.start()
proc2.start()

这只是一个简易模型,来说明队列在进程之间的通信。

这里的队列用法和线程中队列用法基本相同,只是一个用于进程通信,一个用于线程通信。

管道

管道的简单实用:

from multiprocessing import Process, Pipe

def f(conn):
conn.send("Hello world")
conn.close() if __name__ == '__main__':
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,))
p.start()
print(parent_conn.recv())
p.join()

manager

Manager()返回的manager对象控制了一个server进程,此进程包含的python对象可以被其他的进程通过proxies来访问。从而达到多进程间数据通信且安全。

A manager object returned by Manager() controls a server process which holds Python objects and allows other processes to manipulate them using proxies.

A manager returned by Manager() will support types listdictNamespaceLockRLockSemaphoreBoundedSemaphoreConditionEventBarrierQueueValue and Array.

一个简单实用的实例:

from multiprocessing import Process, Manager
import random def f(list):
list.append(random.randint(0,100))
print(list) if __name__ == "__main__":
p_list = []
with Manager() as manager:
l = manager.list()
for i in range(10):
p = Process(target=f,args=(l,))
p.start()
p_list.append(p) for res in p_list:
res.join()

每一个进程给列表中添加一个数据,执行结果如下:

[95]
[95, 25]
[95, 25, 31]
[95, 25, 31, 70]
[95, 25, 31, 70, 9]
[95, 25, 31, 70, 9, 17]
[95, 25, 31, 70, 9, 17, 96]
[95, 25, 31, 70, 9, 17, 96, 71]
[95, 25, 31, 70, 9, 17, 96, 71, 96]
[95, 25, 31, 70, 9, 17, 96, 71, 96, 3]

进程池:

进程池内部维护一个进程序列,当使用时,则去进程池中获取一个进程,如果进程池序列中没有可供使用的进进程,那么程序就会等待,直到进程池中有可用进程为止。

from  multiprocessing import Process, Pool
import time def Foo(i):
time.sleep(2)
return i + 100 def Bar(arg):
print('-->exec done:', arg) if __name__ == "__main__":
pool = Pool(5) for i in range(10):
pool.apply_async(func=Foo, args=(i,), callback=Bar)
# pool.apply(func=Foo, args=(i,)) print('end')
pool.close()
pool.join()

注意:其中在调用apply_async时候使用了callback回调函数

进程池详解,看到的一片博文:https://www.cnblogs.com/qiangyuge/p/7455814.html

python进程编程的更多相关文章

  1. python并发编程之多进程(二):互斥锁(同步锁)&进程其他属性&进程间通信(queue)&生产者消费者模型

    一,互斥锁,同步锁 进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的, 竞争带来的结果就是错乱,如何控制,就是加锁处理 part1:多个进程共享同一打印终 ...

  2. Python 多进程编程之 进程间的通信(在Pool中Queue)

    Python 多进程编程之 进程间的通信(在Pool中Queue) 1,在进程池中进程间的通信,原理与普通进程之间一样,只是引用的方法不同,python对进程池通信有专用的方法 在Manager()中 ...

  3. Python 多进程编程之 进程间的通信(Queue)

    Python 多进程编程之 进程间的通信(Queue) 1,进程间通信Process有时是需要通信的,操作系统提供了很多机制来实现进程之间的通信,而Queue就是其中的一个方法----这是操作系统开辟 ...

  4. python网络编程基础(线程与进程、并行与并发、同步与异步、阻塞与非阻塞、CPU密集型与IO密集型)

    python网络编程基础(线程与进程.并行与并发.同步与异步.阻塞与非阻塞.CPU密集型与IO密集型) 目录 线程与进程 并行与并发 同步与异步 阻塞与非阻塞 CPU密集型与IO密集型 线程与进程 进 ...

  5. 《转载》Python并发编程之线程池/进程池--concurrent.futures模块

    本文转载自Python并发编程之线程池/进程池--concurrent.futures模块 一.关于concurrent.futures模块 Python标准库为我们提供了threading和mult ...

  6. python并发编程之进程、线程、协程的调度原理(六)

    进程.线程和协程的调度和运行原理总结. 系列文章 python并发编程之threading线程(一) python并发编程之multiprocessing进程(二) python并发编程之asynci ...

  7. [ Python - 14 ] python进程及线程编程

    什么是进程: 简单来讲,进程就是操作系统中运行的程序或任务,进程和程序的区别在于进程是动态的,而程序是静态的.进程是操作系统资源管理的最小单位. 什么是线程: 线程是进程的一个实体,是cpu调度和分派 ...

  8. Python进阶(4)_进程与线程 (python并发编程之多进程)

    一.python并发编程之多进程 1.1 multiprocessing模块介绍 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大 ...

  9. Python并发编程05 /死锁现象、递归锁、信号量、GIL锁、计算密集型/IO密集型效率验证、进程池/线程池

    Python并发编程05 /死锁现象.递归锁.信号量.GIL锁.计算密集型/IO密集型效率验证.进程池/线程池 目录 Python并发编程05 /死锁现象.递归锁.信号量.GIL锁.计算密集型/IO密 ...

随机推荐

  1. HDU 1754 - I Hate It & UVA 12299 - RMQ with Shifts - [单点/区间修改、区间查询线段树]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 Time Limit: 9000/3000 MS (Java/Others) Memory Li ...

  2. Win10 下 RabbitMQ 的 安装 配置

    记录下本人在win10环境下安装RabbitMQ的步骤,以作备忘. 第一步:下载并安装erlang 原因:RabbitMQ服务端代码是使用并发式语言Erlang编写的,安装Rabbit MQ的前提是安 ...

  3. linux系统下top命令参数详解

    简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按 ...

  4. 洛谷P1083 借教室 NOIP2012D2T2 线段树

    正解:线段树 解题报告: ...真的不难啊只是开了这个坑就填下? 就是先读入每天的教室数建个线段树然后每次读入就update一下,线段树存的就这一段的最小值啊,然后如果有次更新完之后tr[1]小于0了 ...

  5. linux平台mysql密码设置

    登录mysql默认没有指定账号 查看默认账号是谁 select user(); mysql> select user();+----------------+| user() |+------- ...

  6. android studio 自定义控件

    第一种方式: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...

  7. 解决PHPStorm经常卡顿现象 调整内存限制

    https://www.jisec.com/other/451.html 为什么调整内存? 最近发现PHPstorm在打开一些大点的js, html文件时,会非常的卡顿,这个主要的原因是因为设置的内存 ...

  8. Spark性能优化(一)

    前言 在大数据计算领域,Spark已经成为了越来越流行.越来越受欢迎的计算平台之一.Spark的功能涵盖了大数据领域的离线批处理.SQL类处理.流式/实时计算.机器学习.图计算等各种不同类型的计算操作 ...

  9. [py][mx]django分页第三方模块django-pure-pagination

    前台的这些数据都是从后台取来的 分页模块django-pure-pagination - 一款基于django pagination封装的更好用的分页模块 https://github.com/jam ...

  10. (转)使用git stash解决git pull时的冲突

    在使用git pull代码时,经常会碰到有冲突的情况,提示如下信息: error: Your local changes to 'c/environ.c' would be overwritten b ...