• Python之多进程


    python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程。Python提供了非常好用的多进程包multiprocessing,只需要定义一个函数,Python会完成其他所有事情。借助这个包,可以轻松完成从单进程到并发执行的转换。multiprocessing支持子进程、通信和共享数据、执行不同形式的同步,提供了Process、Queue、Pipe、Lock等组件。

    1. Process

    创建进程的类:Process([group [, target [, name [, args [, kwargs]]]]]),target表示调用对象,args表示调用对象的位置参数元组。kwargs表示调用对象的字典。name为别名。group实质上不使用。
    方法:is_alive() 、join([timeout])、run()、start()、terminate()。其中,Process以start()启动某个进程。

    is_alive():判断该进程是否还活着

    join([timeout]):主进程阻塞,等待子进程的退出, join方法要在close或terminate之后使用。

    run():进程p调用start()时,自动调用run()

    属性:authkey、daemon(要通过start()设置)、exitcode(进程在运行时为None、如果为–N,表示被信号N结束)、name、pid。其中daemon是父进程终止后自动终止,且自己不能产生新进程,必须在start()之前设置。

    例1.1:创建函数并将其作为单个进程

    1. import multiprocessing
    2. import time
    3. def worker(interval):
    4. n = 5
    5. while n > 0:
    6. print("The time is {0}".format(time.ctime())) #输出时间的格式
    7. time.sleep(interval)
    8. n -= 1
    9. if __name__ == "__main__":
    10. p = multiprocessing.Process(target = worker, args = (3,))
    11. p.start()
    12. print "p.pid:", p.pid
    13. print "p.name:", p.name
    14. print "p.is_alive:", p.is_alive()

    结果

    1

    2

    3

    4

    5

    6

    7

    8

    p.pid: 8736

    p.name: Process-1

    p.is_alive: True

    The time is Tue Apr 21 20:55:12 2015

    The time is Tue Apr 21 20:55:15 2015

    The time is Tue Apr 21 20:55:18 2015

    The time is Tue Apr 21 20:55:21 2015

    The time is Tue Apr 21 20:55:24 2015

    例1.2:创建函数并将其作为多个进程

    1. import multiprocessing
    2. import time
    3. def worker_1(interval):
    4. print "worker_1"
    5. time.sleep(interval)
    6. print "end worker_1"
    7. def worker_2(interval):
    8. print "worker_2"
    9. time.sleep(interval)
    10. print "end worker_2"
    11. def worker_3(interval):
    12. print "worker_3"
    13. time.sleep(interval)
    14. print "end worker_3"
    15. if __name__ == "__main__":
    16.   p1 = Process(target=worker_1, args=(6,))
    17. p2 = Process(target=worker_2, args=(4,))
    18. p3 = Process(target=worker_3, args=(2,))
    19.   p1.start() p2.start() p3.start()
    20.   print("The number of CPU is:" + str(cpu_count()))
    21.    for p in active_children():
    22.    print("child p.name:=%s" % p.name + "\tp.id=%s" % str(p.pid))
    23.   print(p1.pid)
    24.   print("END-----")

    结果

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    The number of CPU is:4
    child p.name:=Process-2 p.id=3864
    child p.name:=Process-3 p.id=3256
    child p.name:=Process-1 p.id=7336
    7336
    END-----
    worker_1
    worker_2
    worker_3
    end worker_3
    end worker_2
    end worker_1

    例1.3:将进程定义为类

    1. import multiprocessing
    2. import time
    3. class ClockProcess(multiprocessing.Process):
    4. def __init__(self, interval):
    5. multiprocessing.Process.__init__(self)
    6. self.interval = interval
    7. def run(self):
    8. n = 5
    9. while n > 0:
    10. print("the time is {0}".format(time.ctime()))
    11. time.sleep(self.interval)
    12. n -= 1
    13. if __name__ == '__main__':
    14. p = ClockProcess(3)
    15. p.start()

    :进程p调用start()时,自动调用run()

    结果

    1

    2

    3

    4

    5

    the time is Tue Apr 21 20:31:30 2015

    the time is Tue Apr 21 20:31:33 2015

    the time is Tue Apr 21 20:31:36 2015

    the time is Tue Apr 21 20:31:39 2015

    the time is Tue Apr 21 20:31:42 2015

    例1.4:daemon程序对比结果

    #1.4-1 

  • 相关阅读:
    Docker搭建RK3568开发环境
    Mac/Wins Matlab如何查看APPs源码
    职场高薪 |「中高级测试」面试题
    牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
    华为存储培训
    黑苹果之技嘉(GIGABYTE)主板BIOS设置篇
    java版工程管理系统Spring Cloud+Spring Boot+Mybatis实现工程管理系统源码
    前端 a链接 如何实现下载功能
    内核开发-同步场景与概念
    【JavaEE】网络原理: HTTP协议相关内容
  • 原文地址:https://blog.csdn.net/m0_72557783/article/details/128121385