zoukankan      html  css  js  c++  java
  • Python 多进程

    因为 GIL (Global Interpreter Lock,全局解释器锁) 的关系,python中的多线程其实并不是真正的多线程,想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程。
    使用多进程时,应注意以下几点:

    • 在UNIX平台上,当某个进程终结之后,该进程需要被其父进程调用wait,否则进程成为僵尸进程(Zombie)。所以,有必要对每个Process对象调用join()方法 (实际上等同于wait)。对于多线程来说,由于只有一个进程,所以不存在此必要性。
    • multiprocessing提供了threading包中没有的 IPC (比如Pipe和Queue),效率上更高。应优先考虑Pipe和Queue,避免使用Lock/Event/Semaphore/Condition等同步方式 (因为它们占据的不是用户进程的资源)。
    • 多进程应该避免共享资源。在多线程中,我们可以比较容易地共享资源,比如使用全局变量或者传递参数。在多进程情况下,由于每个进程有自己独立的内存空间,以上方法并不合适。此时我们可以通过共享内存和Manager的方法来共享资源。但这样做提高了程序的复杂度,并因为同步的需要而降低了程序的效率。
    • Process.PID中保存有PID,如果进程还没有start(),则PID为None。
    • window系统下,需要注意的是要想启动一个子进程,必须加上那句if __ name __ == "main",进程相关的要写在这句下面。

    如何创建多进程

    方式一

    from multiprocessing import Process
    import time
    
    def f(name):
        time.sleep(1)
        print('hello', name,time.ctime())
    
    if __name__ == '__main__':
        p_list = []
        for i in range(3):
            p = Process(target=f, args=('alvin',))
            p_list.append(p)
            p.start()
        for i in p_list:
            p.join()
    
        print('end')
    
    运行结果:
    hello alvin Sun Aug 12 09:34:42 2018
    hello alvin Sun Aug 12 09:34:42 2018
    hello alvin Sun Aug 12 09:34:42 2018
    end
    

    方式二:

    from multiprocessing import Process
    import time
    
    class MyProcess(Process):
        def __init__(self):
            super(MyProcess, self).__init__()
    
        def run(self):
            time.sleep(1)
            print('hello', self.name,time.ctime())
    
    if __name__ == '__main__':
        p_list=[]
        for i in range(3):
            p = MyProcess()
            p.start()
            p_list.append(p)
        for p in p_list:
            p.join()
    
        print('end')
    
    运行结果:
    hello MyProcess-1 Sun Aug 12 09:45:07 2018
    hello MyProcess-2 Sun Aug 12 09:45:07 2018
    hello MyProcess-3 Sun Aug 12 09:45:07 2018
    end
    

    获取程序的父进程和子进程

    
    from  multiprocessing import Process
    import os, time
    
    def info(title):
        print(title)
        print('module name: ', __name__)
        print('parent process: ', os.getppid())
        print('process id: ', os.getpid())
    
    def f(name):
        info('33[31;1mfunction f33[0m')
        print('hello', name)
    
    if __name__ == '__main__':
        info('33[32;1mmain process line 33[0m')
        time.sleep(100)
        p = Process(target=info, args=('bob',))
        p.start()
        p.join()
    
    运行结果:
    main process line 
    module name:  __main__
    parent process:  12248
    process id:  1956
    

    参考: http://www.cnblogs.com/yuanchenqi/articles/5745958.html

  • 相关阅读:
    理解和学习qml
    (离线)英语词典软件推荐
    Ubuntu:安装deb文件包以及deb卸载
    mac man汉化方法
    Linux中文件和目录的权限(r, w, x)
    解决mac休眠掉电的解决方法
    线程池之ThreadPool与ForkJoinPool
    程序员的知识焦虑
    回顾2018,展望2019
    NIO基础学习——缓冲区
  • 原文地址:https://www.cnblogs.com/klvchen/p/9461568.html
Copyright © 2011-2022 走看看