python之multiprocessing
创建单个进程示例:
from multiprocessing import Process
import time def add(a, b): print("starting to job") print(a + b) time.sleep(1) print("stoping to job") if __name__ == '__main__': time1 = time.time() # 创建进程 p1 = Process(target=add, args=(1, 2)) # 启动进程 p1.start() # 连接进程 p1.join() time2 = time.time() print("执行程序花费时长:{}".format(time2-time1))
使用for循环创建多个进程:
import time from multiprocessing import Process def sleep(): print("starting to sleep") time.sleep(1) print("stoping to sleep") if __name__ == '__main__': time1 = time.time() process_list = list() for i in range(10): p = Process(target=sleep) p.start() process_list.append(p) for p in process_list: p.join() time2 = time.time() print("程序执行时间为:{}".format(time2-time1))
线程池示例:
from multiprocessing import Pool def number(num): print("worker %d" % num) if __name__ == '__main__': # 创建线程池 pool = Pool(4) # 启动线程池中的进程 pool.map(number, range(5)) # 关闭进程池 pool.close() pool.join()
队列示例:
from multiprocessing import Queue def add(q, x, y): result = x + y q.put(result) def get_result(q): while True: d = q.get() if d is None: break print(d) if __name__ == '__main__': # 创建队列 q = Queue() p1 = Process(target=add, args=(q, 1, 4)) p2 = Process(target=get_result, args=(q,)) p1.start() p2.start() p1.join() # 发送结束信号 q.put(None) p2.join()
管道示例:
from multiprocessing import Pipe def send_message(conn): for i in range(5): conn.send(i) conn.close() def get_message(conn): while True: msg = conn.recv() if msg is None: break print(msg) if __name__ == '__main__': # 创建管道 conn1, conn2 = Pipe() p1 = Process(target=send_message, args=(conn1,)) p2 = Process(target=get_message, args=(conn2,)) p1.start() p2.start() p1.join() # 发送结束信号 conn1.send(None) p2.join()
共享内存 Value示例:
from multiprocessing import Value def job1(n): n.value += 1 print('job1:', n.value) def job2(n): n.value -= 1 print('job2:', n.value) if __name__ == '__main__': n = Value('i', 5) p1 = Process(target=job1, args=(n,)) p2 = Process(target=job2, args=(n,)) p1.start() p2.start() p1.join() p2.join()
共享内存 Array示例:
from multiprocessing import Array def job1(n): for i in n: print("job1:", i) def job2(n): p = n[3] print("job2:", p) if __name__ == '__main__': n = Array("i", [5, 4, 3, 2, 1]) p1 = Process(target=job1, args=(n,)) p2 = Process(target=job2, args=(n,)) p1.start() p2.start() p1.join() p2.join()
进程锁示例:
from multiprocessing import Lock def job1(l, i): l.acquire() try: print("hello world", i) except: l.release() pass if __name__ == '__main__': process_list = list() l = Lock() for i in range(6): p1 = Process(target=job1, args=(l, i,)) p1.start() process_list.append(p1) for p in process_list: p.join()