1. Two ways to open a process
Way One
def task(name): print(" %s start..." % name)if __name__ == ‘__main__‘: p = Process(target=task, args=("sb",)) p.start()
Way Two
class Piao(Process): def __init__(self, name): super().__init__() self.name = name def run(self): print(‘%s start..‘ % self.name)if __name__ == ‘__main__‘: p1 = Piao("sb") p1.start()
Terminate and Is_alive
from multiprocessing import Processimport timedef task(name): time.sleep(1) print("%s done.." % name) if __name__ == ‘__main__‘: p1 = Process(target=task, args=("sb",)) p1.start() p1.terminate() # 发送关闭进程命令 print(p1.is_alive()) # 查看进程是否活动 # True print("主") time.sleep(1)
Name and PID
from multiprocessing import Processimport osdef task(name): print("%s start..." % name) if __name__ == ‘__main__‘: p = Process(target=task, args=("sb",), name="子进程1") # 可以用关键参数来指定进程名 p.start() print(p.name, p.pid, os.getppid()) # p.ppid 报错 print(os.getpid()) # p.pid==os.getpid()
Daemon process
First, the daemon is terminated at the end of the main process execution
Second, the daemon cannot be opened in the process.
from multiprocessing import Processimport timedef foo(): # 不执行 print(123) time.sleep(1) print("end123")def bar(): print(456) time.sleep(3) print("end456")if __name__ == ‘__main__‘: p1 = Process(target=foo) p2 = Process(target=bar) p1.daemon = True p1.start() p2.start() print("main-------")
Mutual exclusion Lock
foreplay: Data is not shared between processes, but sharing the same file system, so access to the same file, or open the same print terminal, sharing brings competition.
Mutex: mutual exclusion is the meaning of mutually exclusive lock. Ensure that the data security is not garbled.
from multiprocessing import Processfrom multiprocessing import Lockimport timedef task(name, mutex): mutex.acquire() print("%s 1" % name) time.sleep(1) print("%s 2" % name) mutex.release() if __name__ == ‘__main__‘: mutex = Lock() for i in range(2): p = Process(target=task, args=("进程%s" % i, mutex)) p.start()
The difference between mutexes and joins
The mutex is to make the lock part into a string, join is to make the whole code into serial.
#!/usr/bin/env python# -*- coding: utf-8 -*-# __author__:JasonLINfrom multiprocessing import Process, Lockimport timeimport jsondef search(name): time.sleep(1) data = json.load(open("data.txt", "r")) print("<%s>剩余票数[%s]" % (name, data["count"])) def buy(name): data = json.load(open("data.txt", "r")) if int(data["count"]) > 0: data["count"] -= 1 time.sleep(2) json.dump(data, open("data.txt", "w")) print("%s 购票成功" % name) def task(name, mutex): search(name) mutex.acquire() buy(name) mutex.release() if __name__ == ‘__main__‘: mutex = Lock() for i in range(10): p = Process(target=task, args=("路人%s" % i, mutex)) p.start()
Join method, search and buy both become serial, less efficient.
from multiprocessing import Processimport timeimport jsondef search(name): data = json.load(open("data.txt", "r")) time.sleep(1) print("<%s>剩余票数[%s]" % (name, data["count"]))def buy(name): data = json.load(open("data.txt", "r")) if int(data["count"]) > 0: data["count"] -= 1 time.sleep(2) json.dump(data, open("data.txt", "w")) print("%s 购票成功" % name) def task(name): search(name) buy(name)if __name__ == ‘__main__‘: for i in range(10): p = Process(target=task, args=("路人%s" % i,)) p.start() p.join()
Queue
Processes are isolated from each other, and to implement interprocess communication (IPC), the Multiprocessing module supports two forms: queues and pipelines, both of which are delivered using messaging.
Create a queue class (the underlying is implemented as a pipe and lock)
Queue([maxsize]):创建共享的进程队列,Queue是多进程安全的队列,可以使用Queue实现多进程之间的数据传递Queue([maxsize]):是队列中允许最大项数,省略则无大小限制q.put方法用以插入数据到队列中。q.get方法可以从队列读取并且删除一个元素。
Code instance
from multiprocessing import Process,Queueq=Queue(3)#put ,get ,put_nowait,get_nowait,full,emptyq.put(1)q.put(2)q.put(3)print(q.full()) #满了# q.put(4) #再放就阻塞住了print(q.get())print(q.get())print(q.get())print(q.empty()) #空了# print(q.get()) #再取就阻塞住了
Producer Consumer Model
Why use a producer consumer model?
Producers refer to the task of producing data, the consumer is the task of processing data, in concurrent programming, if the producer processing speed quickly, and the consumer processing speed is very slow, then the producer must wait for the consumer to finish processing, can continue production data. Similarly, consumers must wait for producers if their processing power is greater than that of producers. To solve this problem, the producer and consumer models were introduced.
What is the producer and consumer model?
The producer-consumer model solves the problem of strong coupling between producers and consumers through a container. Producers and consumers do not communicate with each other directly, but by blocking the queue.
This blocking queue is used to decouple producers and consumers.
Turn from:
https://www.luffycity.com/python-book/di-7-zhang-bing-fa-bian-cheng/72-bing-fa-bian-cheng-zhi-duo-jin-cheng/ 727-sheng-chan-zhe-xiao-fei-zhe-mo-xing.html
From multiprocessing import Process, Queueimport timeimport randomdef producer (q): For I in range (3): res = "Bun%" S "% i time.sleep (0.5) print (" produced%s "% res) Q.put (RES) def consume (q): While true:res = Q.get () If not res:break time.sleep (Random.randint (1, 3)) print ("ate%s"% res) if __name__ = = ' __main__ ': q = Queue () p1 = Process (Target=producer, args= (q,)) P2 = Process (Target=producer, args= ( Q,)) P3 = Process (Target=producer, args= (Q,)) C1 = Process (Target=consume, args= (q,)) C2 = Process (Target=con Sume, args= (Q,)) P1.start () P2.start () P3.start () C1.start () C2.start () P1.join () P2.join () P3 . Join () Q.put (none) Q.put (none) print ("Zhu") from multiprocessing import Process, Queueimport timeimport randomdef Producer (q): For I in range (3): res = "Bun%s"% i time.sleep (0.5) print ("%s produced"% res) Q.PU T (RES) def consume (q): While true:res = Q.get () if not res:break time.sleep (Random.randint (1, 3)) p Rint ("ate%s"% res) if __name__ = = ' __main__ ': q = Queue () p1 = Process (Target=producer, args= (q,)) P2 = Pr Ocess (Target=producer, args= (q,)) P3 = Process (Target=producer, args= (Q,)) C1 = Process (Target=consume, args= (q, )) C2 = Process (Target=consume, args= (Q,)) P1.start () P2.start () P3.start () C1.start () C2.start () P1.join () P2.join () P3.join () q.put (none) Q.put (none) print ("Zhu")
Joinablequeue ([maxsize])
from multiprocessing import Process, JoinableQueueimport timeimport randomdef producer(q): for i in range(3): res = "包子%s" % i time.sleep(0.5) print("生产了%s" % res) q.put(res) q.join() # 等消费者把所有数据取走之后,生产者才结束 def consume(q): while True: res = q.get() if not res: break time.sleep(random.randint(1, 3)) print("吃了%s" % res) q.task_done() # 发送信号给q.join(),说明已经从队列中取走一个数据并处理完毕if __name__ == ‘__main__‘: q = JoinableQueue() p1 = Process(target=producer, args=(q,)) p2 = Process(target=producer, args=(q,)) p3 = Process(target=producer, args=(q,)) c1 = Process(target=consume, args=(q,)) c2 = Process(target=consume, args=(q,)) c1.daemon = True c2.daemon = True p1.start() p2.start() p3.start() c1.start() c2.start() p1.join() p2.join() p3.join() print("zhu")
Python process