进程
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# -*- coding: utf-8 -*- """ Created on Wed Jul 29 21:04:07 2020 @author: Administrator """ #方式一 # from multiprocessing import Process # # def out(name): # print("%s开始"%(name)) # print("%s结束"%(name)) # pass # # if __name__ == "__main__": # p1 = Process(target=out,args=("任务一",)) # p1.start() # p2 = Process(target=out, args=("任务二",)) # p2.start() # print("主函数") # 方法二 # from multiprocessing import Process # # class MyProcess(Process): # def __init__(self,name): # super().__init__() # self.name = name # # def run(self): # print("%s开始"%(self.name)) # print("%s结束"%(self.name)) # # if __name__ == "__main__": # mp1 = MyProcess("任务一") # mp2 = MyProcess("任务二") # mp1.start() # mp2.start() # print("主函数") #查看进程编号 # from multiprocessing import Process # import os,time # # def test(): # print("%s开启 他的父进程是:%s"%(os.getpid(),os.getppid())) # time.sleep(3) # print("%s结束" % (os.getpid())) # # if __name__ == "__main__": # p1 = Process(target=test) # p1.start() # p2 = Process(target=test) # p2.start() # print("主函数:%s 父进程:%s"%(os.getpid(),os.getppid())) # 父进程结束,留下子进程,就叫孤儿进程 ,就给孤儿院init了 # 父进程一直不结束,子进程结束,就叫僵尸进程,不好 # from multiprocessing import Process # import os # import time # import mp3play # from pynput.keyboard import Listener # # # def p_out(): # while True: # print("\r",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),end="") # time.sleep(1) # # def i_int(): # # s = input("请输入") # # print(s) # def press(key): # print("heihei",key.char) # # def playmusic(): # mp3 = mp3play.load("http://fs.pc.kugou.com/202007312251/f26b3b50daee07c4ca1be01819be1e0f/G228/M04/0D/13/xJQEAF70gTWAIbMLADXSnM_FbtE258.mp3") # mp3.play() # time.sleep(int(mp3.seconds())) # # pass # if __name__ == "__main__": # # p1 = Process(target=p_out) # # p1.start() # # p2 =Process(target=i_int) # # p2.start() # p3 = Process(target=playmusic) # p3.start() # with Listener(on_press = press) as listener: # listener.join() # 进程的其他属性 # from multiprocessing import Process # import time # # def task(name): # print("%s is running"%(name)) # time.sleep(1) # # if __name__ == "__main__": # p1 = Process(target=task,args=("任务一",)) # p2 = Process(target=task, args=("任务二",)) # p3 = Process(target=task, args=("任务三",)) # p1.start() # 给操作系发信号,并不是立刻执行 # p2.start() # p3.start() # print("主进程开启") # p1.join() #发个信号,join 的作用 就是 等待僵尸进程结束,才会执行后面的代码 # p2.join() # p3.join() # p1.is_alive() #判断进程是否存活 # p1.terminate() #发信号 告诉操作系统,回收这个 # p1.name() #创建的时候 不指定 那就是默认的 # print("主进程结束") #守护进程:父进程死了,子进程立马跟着死,就叫守护进程 # from multiprocessing import Process # import time # # def task(name): # time.sleep((2)) # print("%s is running"%(name)) # # if __name__ == "__main__": # p1 = Process(target=task,args=("进程一",)) # p1.daemon = True # 守护进程 守护进程内部不可以调用进程 # p1.start() # print("主进程") #互斥锁: 三个进程开始抢,抢了以后就执行完一个再继续抢,变成并发了,跟join的区别,这个是随机抢锁 # from multiprocessing import Process # from multiprocessing import Lock # # def task(name,mutex): # mutex.acquire() # print("%s 第一步"%(name)) # print("%s 第二步"%(name)) # print("%s 第三步"%(name)) # mutex.release() # if __name__ == "__main__": # mutex = Lock() # for i in range(1,4): # p = Process(target=task,args=("任务%s"%i,mutex,)) # p.start() # 队列:把互斥锁封装了 主要用在生产者与消费者模型 # 以后不会用这个,稳定性太差,用Rabbitmq 实现生产者与消费者 # from multiprocessing import Process,Queue # import time # import random # # def producer(q): # for i in range(1,10): # t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) # print("生产者在%s时生产了包子%s"%(t,i)) # time.sleep(random.randint(0,4)) # q.put(i) # # def consumer(q): # while True: # res = q.get() # if res == None: # break # else: # # t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) # print("消费者在%s时吃了包子%s" % (t,res)) # # # if __name__ == "__main__": # q = Queue() # pro = Process(target=producer,args=(q,)) # pro.start() # con = Process(target=consumer,args=(q,)) # con.start() # pro.join() # q.put(None) #有几个消费者就几行这个代码 # print("主程序开始") # #这个库,能优化上面的代码,用到守护进程,q.join() q.task_done # from multiprocessing import JoinableQueue
进程结构:
1.两种启动方式
2.查看进程编号
3.僵尸进程和孤儿进程
4.进程的其他属性
5.守护进程
6.互斥锁
7.队列
正题来了=====================
"""
# from multiprocessing import Process,current_process,Lock,Queue
# from concurrent.futures import ProcessPoolExecutor
# import time,random,os
# from multiprocessing import Queue,Lock
发个信号,进程我交给你了,你愿意啥时候执行都行
Process.start()
发个信号,你必须等我进程结束以后才可以执行下面的操作
Process.join()
获取进程名字
Process.name
current_process().name
获取进程pid
current_process().pid
os.getpid()
获取父进程pid
os.getppid()
设置守护进程
process.daemon = True
current_process().daemon = True # 不会用,用上面那个
判断进程是否存活
Process.is_alive()
告诉操作系统回收这个进程
Process.terminate() #英文是停止的意思
互斥锁:
from multiprocessing import Lock
mutex = Lock()
mutex.acquire()
mutex.release()
队列实现生产者与消费者:
不常用这个,以后用Rabbitmq实现生产者与消费者
你给我记住:进程之间数据不是共享的,靠队列去解决
from multiprocessing import Queue,Lock
Queue(3).put()
Queue(3).get()
进程池:解决进程太多的问题
进程池不可以与队列和锁一起使用(目前看法,欢迎指正)
pps = ProcessPoolExecutor(最大进程数)
pps.submit(fun2,参数一,参数二)
pps.shutdown() # 相当于join
"""
-----------------------------------------------------------------------------------------------------------------------------------------