zoukankan      html  css  js  c++  java
  • Python学习-day10 进程

    学习完线程,学习进程

    进程和线程的语法有很多一样的地方,不过在操作系统中的差别确实很大。

    模块是threading 和 multiprocessing

    多进程multiprocessing

    multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.

    如何启动多进程

    #Authon Ivor
    from multiprocessing import Process
    import os
    
    def info(title):
        print(title)
        print('module name:', __name__)
        print('parent process:', os.getppid())
        print('process id:', os.getpid())
        print("")
    
    def f(name):
        info('33[31;1mfunction f33[0m')
        print('hello', name)
    
    if __name__ == '__main__':
        info('33[32;1mmain process line33[0m')
        p = Process(target=f, args=('bob',))
        p.start()
        p.join()
    View Code


    进程间通信的三种方式

    Queue

    #Author:Ivor
    from multiprocessing import Process,Queue
    import os
    # threading.queue.Queue()
    def run(q):
        q.put("---in the Child process---")
        print("parent_pid:",os.getppid())
        print("current_pid:",os.getpid())
        print("------------------")
    
    
    if __name__ == '__main__':
        q = Queue()
        print("---main process---")
        print("parent_pid:",os.getppid())
        print("current_pid:",os.getpid())
        print("------------------")
        p = Process(target=run,args=(q,))
        p.start()
        print(q.get())
        p.join()
    View Code

    Pipe

    #Author:Ivor
    from multiprocessing import Process,Pipe
    
    def run(conn):
        conn.send("from child")
        conn.close()
    
    if __name__ == '__main__':
        parent_conn,child_conn = Pipe()
        p = Process(target=run,args=(child_conn,))
        p.start()
        print(parent_conn.recv())
        p.join()
    View Code

    Manager

    #Author:Ivor
    from multiprocessing import Process,Manager
    import os
    def run(d,l):
        d[os.getpid()] = os.getpid()
        l.append(os.getpid())
        print(l)
    
    pro_list = []
    if __name__ == '__main__':
        manager = Manager()
        d = manager.dict()
        l = manager.list()
        for i in range(10):
            p = Process(target=run,args=(d,l))
            pro_list.append(p)
            p.start()
        for i in pro_list:
            i.join()
        print(d)
        print(l)
    View Code


    进程池的概念

    Pool

    #Authon Ivor
    from multiprocessing import Process,Pool
    import time,os
    def run(n):
        print("Process %s is running.." % n)
        time.sleep(1)
        return os.getpid()
    
    def bar(arg):
        print("exec done---",arg)
    
    result = []
    if __name__ == '__main__':
        pool = Pool(processes=2)
        for n in range(10):
            result.append(pool.apply_async(func=run,args=(n,),callback=bar))
        for res in result:
            print("res---",res.get())
        pool.close()
        pool.join()
    View Code
  • 相关阅读:
    团队作业开发过程
    UVM基础之--------uvm_root
    UVM基础之------uvm_transaction
    UVM基础之----uvm_object
    UVM挑战及概述
    定制UVM Messages(参考)
    SV creation order
    IC验证概念总结
    win7 硬盘安装suse双系统启动顺序更改
    suse 下的gcc安装
  • 原文地址:https://www.cnblogs.com/Darksugar/p/6738580.html
Copyright © 2011-2022 走看看