龙空技术网

5分钟掌握Python(十五)之多进程

小文述说 142

前言:

当前你们对“python主进程关闭所有子进程”大概比较讲究,你们都想要了解一些“python主进程关闭所有子进程”的相关内容。那么小编也在网摘上汇集了一些有关“python主进程关闭所有子进程””的相关文章,希望兄弟们能喜欢,同学们一起来了解一下吧!

1)类 Process:

eg

#!/usr/bin/env python3# -*- coding: UTF-8 -*-import multiprocessingimport timedef worker(interval, name):    print(name + '【start】')    time.sleep(interval)    print(name + '【end】')if __name__ == "__main__":    p1 = multiprocessing.Process(target=worker, args=(2, '两点水1'))    p2 = multiprocessing.Process(target=worker, args=(3, '两点水2'))    p3 = multiprocessing.Process(target=worker, args=(4, '两点水3'))    p1.start()    p2.start()    p3.start()    print("The number of CPU is:" + str(multiprocessing.cpu_count()))    for p in multiprocessing.active_children():        print("child   p.name:" + p.name + "\tp.id" + str(p.pid))    print("END!!!!!!!!!!!!!!!!!")

2)把进程创建成类:

eg

# -*- coding: UTF-8 -*-import multiprocessingimport timeclass ClockProcess(multiprocessing.Process):    def __init__(self, interval):        multiprocessing.Process.__init__(self)        self.interval = interval    def run(self):        n = 5        while n > 0:            print("当前时间: {0}".format(time.ctime()))            time.sleep(self.interval)            n -= 1if __name__ == '__main__':    p = ClockProcess(3)    p.start()

3)daemon 属性:

eg

# -*- coding: UTF-8 -*-import multiprocessingimport timedef worker(interval):    print('工作开始时间:{0}'.format(time.ctime()))    time.sleep(interval)    print('工作结果时间:{0}'.format(time.ctime()))if __name__ == '__main__':    p = multiprocessing.Process(target=worker, args=(3,))    p.daemon = True    p.start()    print('【EMD】')

4)join 方法:

eg

import multiprocessingimport timedef worker(interval):    print('工作开始时间:{0}'.format(time.ctime()))    time.sleep(interval)    print('工作结果时间:{0}'.format(time.ctime()))if __name__ == '__main__':    p = multiprocessing.Process(target=worker, args=(3,))    p.daemon = True    p.start()    p.join()    print('【EMD】')

5)Pool:

eg

# -*- coding: UTF-8 -*-from multiprocessing import Poolimport os, time, randomdef long_time_task(name):    print('进程的名称:{0} ;进程的PID: {1} '.format(name, os.getpid()))    start = time.time()    time.sleep(random.random() * 3)    end = time.time()    print('进程 {0} 运行了 {1} 秒'.format(name, (end - start)))if __name__ == '__main__':    print('主进程的 PID:{0}'.format(os.getpid()))    p = Pool(4)    for i in range(6):        p.apply_async(long_time_task, args=(i,))    p.close()    # 等待所有子进程结束后在关闭主进程    p.join()    print('【End】')

6)进程间通信:

eg

#!/usr/bin/env python3# -*- coding: UTF-8 -*-from multiprocessing import Process, Queueimport os, time, randomdef write(q):    # 写数据进程    print('写进程的PID:{0}'.format(os.getpid()))    for value in ['两点水', '三点水', '四点水']:        print('写进 Queue 的值为:{0}'.format(value))        q.put(value)        time.sleep(random.random())def read(q):    # 读取数据进程    print('读进程的PID:{0}'.format(os.getpid()))    while True:        value = q.get(True)        print('从 Queue 读取的值为:{0}'.format(value))if __name__ == '__main__':    # 父进程创建 Queue,并传给各个子进程    q = Queue()    pw = Process(target=write, args=(q,))    pr = Process(target=read, args=(q,))    # 启动子进程 pw    pw.start()    # 启动子进程pr    pr.start()    # 等待pw结束:    pw.join()    # pr 进程里是死循环,无法等待其结束,只能强行终止    pr.terminate()

标签: #python主进程关闭所有子进程 #python 多进程关闭 #python主进程读取子进程数据