版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_40313634/article/details/100937313
1. 程序中另起进程
代码语言:javascript复制from multiprocessing import Process
from os import getpid
from time import sleep
def run_time(name):
sleep(5)
print('child progress {}: {}'.format(name, getpid()))
if __name__ == '__main__':
print('parent progress id: {}'.format(getppid())) # os.getpid(): 获取当前进程ID; os.getppid(): 获取父进程ID
p = Process(target=run_time, args=('hello_world',)) # 初始化子进程:target=目前函数名, args=参数
print('start child progress')
p.start() # 启动子进程
p.join() # 进程同步:等待子进程执行完毕,程序再向下执行
print('end child progress')
方法
- getppid()、getpid()
- p.start()
- p.join()
- Process(target=, args=(,))
- Pool(n)
- p.apply_async(, args=(,))
- subprocess.call([’’, ‘’])
- Queue()
- q.put(value)
- q.get(True)
2. 进程池
代码语言:javascript复制from multiprocessing import Pool
from os import getpid
from time import sleep
def long_time_task(name):
sleep(5)
print('child process {0}: {1}n'.format(name, getpid()))
if __name__ == '__main__':
print('parent process: {0}n'.format(getpid()))
p = Pool(2) ## 初始化进程池:一次可并行执行2个进程(等这两个执行结束,再次启动执行接下来的两个,和电脑是几核的无关)
for i in range(10):
p.apply_async(long_time_task, args=(i,)) ## 进程池添加进程
print('start child progressn')
p.close() ## 关闭进程池(不能再添加新进程了),准备执行进程
p.join() # 进程同步
print('end child progressn')
3. 问题:无法获取子进程的执行结果 -> 子进程
代码语言:javascript复制import subprocess
print('nslookup www.python.org')
r = subprocess.call(['nslookup', 'www.python.org'])
print('Exit code: {}'.format(r))
4. 进程间通信
- 队列:Queue
from multiprocessing import Process, Queue
from time import sleep
from random import random
def write(q):
for i in range(5):
print('puts {}'.format(i))
q.put(i)
sleep(1)
def read(q):
sleep(10)
while True:
i = q.get(True)
print('get {}'.format(i))
if __name__ == '__main__':
q = Queue()
pw = Process(target=write, args=(q,))
pr = Process(target=read, args=(q,))
print('START')
pw.start()
pr.start()
pw.join()
while True:
if q.empty(): # 如果队列中的数据都处理完了,则强制停止read方法
pr.terminate()
break
print('END')
- 信号量:Semaphore
from multiprocessing import Process, Semaphore
from time import sleep
def write(f):
f.acquire()
sleep(5)
f.release()
if __name__ == '__main__':
pp = []
f = Semaphore(1)
for i in range(5):
p = Process(target=write, args=(f,))
p.start()
pp.append(p)
for p in pp:
p.join()