【Python】process使用文章介绍了 Python 的 multiprocess 模块在多进程编程中的应用 包括 apply async 实现异步进程池 Manager 创建安全的数据共享 Pool 进程池的使用 以及 Queue Pipe 和 joinableQueu 等数据结构在进程间通信和资源共享的角色
1、apply_async
from multiprocessing import Pool def test(i): print(i) if __name__ == "__main__": pool = Pool(8) for i in range(100): pool.apply_async(test, args=(i,)) print("test") pool.close() pool.join()
2、Manager
import multiprocessing def f(x, arr, l, d, n): x.value = 3.14 arr[0] = 5 l.append('xx') d[1] = 2 n.a = 10 if __name__ == '__main__': server = multiprocessing.Manager() x = server.Value('d', 0.0) arr = server.Array('i', range(10)) l = server.list() d =server.dict() n = server.Namespace() proc = multiprocessing.Process(target=f, args=(x, arr, l, d, n)) proc.start() proc.join() print(x.value) print(arr) print(l) print(d) print(n)
3、Pool进程池
import datetime import os from multiprocessing import Pool from time import sleep def test(i): print(i) sleep(5) print('Run child process (%s)...' % ( os.getpid())) if __name__ == "__main__": lists = range(30) pool = Pool(8) t1=datetime.datetime.now() pool.map(test, lists) pool.close() pool.join() t2 = datetime.datetime.now() print(f'cost==={
t2-t1}')
4、queue资源共享
from multiprocessing import Process, Queue import os import time import random def write(q): print("process to write:{}" .format(os.getpid())) for value in [1, 2, 3]: print("put {} to queue..." .format(value)) q.put(value) time.sleep(random.random()) def read(q): print("process to read:{}" .format(os.getpid())) while True: value = q.get(True) print("get {} from queue..." .format(value)) if __name__ == '__main__': q = Queue() p_w = Process(target=write, args=(q,)) p_r = Process(target=read, args=(q,)) p_w.start() p_r.start() p_w.join() p_r.terminate()
5、Pipe
from multiprocessing import Process, Pipe import time def f(Subconn): time.sleep(1) Subconn.send("吃了吗") print("来自父亲的问候:{}" .format(Subconn.recv())) Subconn.close() if __name__ == '__main__': parent_conn, child_conn = Pipe() p = Process(target=f, args=(child_conn,)) p.start() print("来自儿子的问候:{}" .format(parent_conn.recv())) parent_conn.send("嗯")
6、joinableQueue
from multiprocessing import Process, JoinableQueue import time, random def consumer(q): while True: res = q.get() print('消费者拿到了 %s' % res) q.task_done() def producer(seq, q): for item in seq: time.sleep(random.randrange(1,2)) q.put(item) print('生产者做好了 %s' % item) q.join() if __name__ == "__main__": q = JoinableQueue() seq = ('产品%s' % i for i in range(5)) p = Process(target=consumer, args=(q,)) p.daemon = True p.start() producer(seq, q) print('主线程')
7、Value_Array进程资源共享
import multiprocessing def f(n, a): n.value = 3.14 a[0] = 5 if __name__ == '__main__': num = multiprocessing.Value('d', 0.0) arr = multiprocessing.Array('i', range(10)) p = multiprocessing.Process(target=f, args=(num, arr)) p.start() p.join() print(num.value) print(arr[:])
今天的文章
【Python】process使用分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/103142.html