[notice: All Rights Reserved. Please reprint. Do not use for commercial purposes. Contact email: feixiaoxing@163.com]
Because there are not many opportunities to write multiple processes, we do not pay much attention to this aspect. Therefore, the multi process code has not been mastered. In fact, from the bottom of my heart, I know that multi process has many advantages, among which stability is a very important one. Now we use python to write multiprocesses. It's not difficult to write multiprocessing libraries.
1. Create multi process
t = multiprocessing.Process(target = run_process) t.start()
2. Wait for the process to end
t.join()
3. Create a pipe
(p1, p2) = multiprocessing.Pipe() data = p1.read() p2.send(data)
4. Create lock
Locks under multiprocesses are mostly used to restrict access to certain files. The usage is as follows:,
l = multiprocessing.Lock() l.acquire() l.release()
5. Create semaphore
Semaphore is a good method for process synchronization, and there are many opportunities for actual use. The usage is as follows:,
a = multiprocessing.Semaphore(0)
a.acquire()
a.release()
6. Signal processing
multiprocessing library is a real multiprocess. When using ps aux to detect, you can find that there are many processes running. Similarly, the signal sent to the parent will also be sent to the child process, so generally speaking, the signal functions are common,
signal.signal(signal.SIGINT, sig_process)
7. Complete example
#!/usr/bin/python
import multiprocessing
import os
import time
import signal
g_exit = 0
def sig_process(sig, frame):
global g_exit
g_exit = 1
def send_process(p, l):
data = 0
while not g_exit:
p.send(data)
data += 1
l.release()
time.sleep(1)
l.release()
p.send(0)
def get_process(p, l):
while not g_exit:
l.acquire()
print p.recv()
def main():
signal.signal(signal.SIGINT, sig_process)
l = multiprocessing.Semaphore(0)
(p1, p2) = multiprocessing.Pipe()
t1 = multiprocessing.Process(target = send_process, args=(p1,l,))
t1.start()
t2 = multiprocessing.Process(target = get_process, args=(p2,l,))
t2.start()
while not g_exit:
time.sleep(3)
t2.join()
t1.join()
if __name__ == '__main__':
main()