python进程编程
多进程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 list
, dict
, Namespace
, Lock
, RLock
, Semaphore
, BoundedSemaphore
, Condition
, Event
, Barrier
, Queue
, Value
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进程编程的更多相关文章
- python并发编程之多进程(二):互斥锁(同步锁)&进程其他属性&进程间通信(queue)&生产者消费者模型
一,互斥锁,同步锁 进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的, 竞争带来的结果就是错乱,如何控制,就是加锁处理 part1:多个进程共享同一打印终 ...
- Python 多进程编程之 进程间的通信(在Pool中Queue)
Python 多进程编程之 进程间的通信(在Pool中Queue) 1,在进程池中进程间的通信,原理与普通进程之间一样,只是引用的方法不同,python对进程池通信有专用的方法 在Manager()中 ...
- Python 多进程编程之 进程间的通信(Queue)
Python 多进程编程之 进程间的通信(Queue) 1,进程间通信Process有时是需要通信的,操作系统提供了很多机制来实现进程之间的通信,而Queue就是其中的一个方法----这是操作系统开辟 ...
- python网络编程基础(线程与进程、并行与并发、同步与异步、阻塞与非阻塞、CPU密集型与IO密集型)
python网络编程基础(线程与进程.并行与并发.同步与异步.阻塞与非阻塞.CPU密集型与IO密集型) 目录 线程与进程 并行与并发 同步与异步 阻塞与非阻塞 CPU密集型与IO密集型 线程与进程 进 ...
- 《转载》Python并发编程之线程池/进程池--concurrent.futures模块
本文转载自Python并发编程之线程池/进程池--concurrent.futures模块 一.关于concurrent.futures模块 Python标准库为我们提供了threading和mult ...
- python并发编程之进程、线程、协程的调度原理(六)
进程.线程和协程的调度和运行原理总结. 系列文章 python并发编程之threading线程(一) python并发编程之multiprocessing进程(二) python并发编程之asynci ...
- [ Python - 14 ] python进程及线程编程
什么是进程: 简单来讲,进程就是操作系统中运行的程序或任务,进程和程序的区别在于进程是动态的,而程序是静态的.进程是操作系统资源管理的最小单位. 什么是线程: 线程是进程的一个实体,是cpu调度和分派 ...
- Python进阶(4)_进程与线程 (python并发编程之多进程)
一.python并发编程之多进程 1.1 multiprocessing模块介绍 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大 ...
- Python并发编程05 /死锁现象、递归锁、信号量、GIL锁、计算密集型/IO密集型效率验证、进程池/线程池
Python并发编程05 /死锁现象.递归锁.信号量.GIL锁.计算密集型/IO密集型效率验证.进程池/线程池 目录 Python并发编程05 /死锁现象.递归锁.信号量.GIL锁.计算密集型/IO密 ...
随机推荐
- Redis 如何保持和MySQL数据一致
1. MySQL持久化数据,Redis只读数据 redis在启动之后,从数据库加载数据. 读请求: 不要求强一致性的读请求,走redis,要求强一致性的直接从mysql读取 写请求: 数据首先都写到数 ...
- ELK之写入MySQL数据库
安装MySQL5.6 创建数据库并且授权 create database elk character set utf8 collate utf8_bin; grant all on elk.* to ...
- kafka window环境搭建
1部署zookeeper ZooKeeper的安装模式分为三种,分别为:单机模式(stand-alone).集群模式和集群伪分布模式. 1.运行环境:ZooKeeper 要求 JAVA 6 以上的环境 ...
- CCCC 排座位 图着色问题
1排座位:https://www.patest.cn/contests/gplt/L2-010 2图着色问题 https://www.patest.cn/contests/gplt/L2-023 建图 ...
- linux:文件打包与压缩
学习内容介绍:Linux 上常用的压缩/解压工具,介绍了zip.rar.tar的使用. 先总结一下常用命令: zip: 打包 :zip something.zip something (目录请加 -r ...
- eclipse反编译插件jadClipse安装使用教程
previously:最近在学习Dependency Injection(依赖注入)模式,看了 martin fowler 的 文章(原文:https://martinfowler.com/artic ...
- m4a 转 wav
https://blog.csdn.net/zjm750617105/article/details/80148473 sox 不行, ffmpeg 很麻烦, 考虑用 avconv 很简单: 安装: ...
- HTML标签_head标签
HTML标签分为两种,自闭合标签和主动闭合标签:没有另一半 自动闭合标签,有另一半叫主动闭合标签. 自动闭合标签,只有开头没有结尾,即没有另一半:如<meta charset="UTF ...
- windows上使用logstash-input-jdbc
(一)安装logstash 下载链接 选择下载你要的对应的logstash版本,这个东西解压就能使用了 (二)安装logstash-input-jdbc 就是用执行logstash-plugin.b ...
- Kubernetes容器调度
Kubernetes的调度器是Kubernetes众多组件的一部分,独立于API服务器之外.调度器本身是可插拔的,任何理解调度器和API服务器之间调用关系的工程师都可以编写定制的调度器.本文后面的介绍 ...