當(dāng)前位置:
首頁(yè) >
python 测试multiprocessing多进程
發(fā)布時(shí)間:2025/3/20
46
豆豆
生活随笔
收集整理的這篇文章主要介紹了
python 测试multiprocessing多进程
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 測(cè)試1 multiprocessing函數(shù)的調(diào)用
- 測(cè)試2 pipe
- 測(cè)試queue
測(cè)試1 multiprocessing函數(shù)的調(diào)用
# -*- coding: utf-8 -*- """ @File : 20200312_test_multiprocessing.py @Time : 2020/3/12 16:38 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import os import threading import multiprocessing# Main print('Main:', os.getpid())print('How are you!')# worker function def worker(sign, lock):lock.acquire()print(sign, os.getpid())lock.release()# Multi-thread # record = [] # lock = threading.Lock()# Multi-process record = [] lock = multiprocessing.Lock()if __name__ == '__main__':# for i in range(5):# thread = threading.Thread(target=worker, args=('thread', lock))# thread.start()# record.append(thread)## for thread in record:# thread.join()# 用multiprocessing調(diào)用一下函數(shù)就相當(dāng)于重新導(dǎo)一下包(相當(dāng)于它自己又開(kāi)了個(gè)python?)for i in range(5):process = multiprocessing.Process(target=worker, args=('process', lock))process.start()record.append(process)for process in record:process.join()結(jié)果:
D:\20191031_tensorflow_yolov3\python\python.exe C:/Users/SIQI/Desktop/test_multiprocessing/20200312_test_multiprocessing.py Main: 22560 How are you! Main: 31028 How are you! Main: 30472 How are you! Main: 11780 How are you! process 31028 process 30472 process 11780 Main: 27904 How are you! Main: 30488 How are you! process 27904 process 30488Process finished with exit code 0測(cè)試2 pipe
# -*- coding: utf-8 -*- """ @File : 20200312_測(cè)試pipe.py @Time : 2020/3/13 9:16 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import multiprocessing as muldef proc1(pipe):pipe.send('hello')# print('proc1 rec:', pipe.recv())def proc2(pipe):print('proc2 rec:', pipe.recv())# pipe.send('hello, too')# Build a pipe pipe = mul.Pipe() print(len(pipe)) # 2if __name__ == '__main__':# Pass an end of the pipe to process 1p1 = mul.Process(target=proc1, args=(pipe[0],))# Pass the other end of the pipe to process 2p2 = mul.Process(target=proc2, args=(pipe[1],))p1.start()p2.start()p1.join()p2.join()結(jié)果:
D:\20191031_tensorflow_yolov3\python\python.exe C:/Users/SIQI/Desktop/test_multiprocessing/20200312_測(cè)試pipe.py 2 2 2 proc2 rec: helloProcess finished with exit code 0測(cè)試queue
參考文章1:Python multiprocessing使用詳解
參考文章2:Python多進(jìn)程編程-進(jìn)程間共享 對(duì)象
參考文章3:python 多進(jìn)程multiprocessing 如何獲取子進(jìn)程的返回值?進(jìn)程池pool
參考文章4:關(guān)于python進(jìn)程池先close再join的疑惑
總結(jié)
以上是生活随笔為你收集整理的python 测试multiprocessing多进程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 线程中start()与run()的区别
- 下一篇: python multiprocessi