• 进程


    进程
    以一个整体的形式暴露给操作系统管理, 里面包含对各种资源的调用, 是资源的集合
    两个进程不能使用一块内存
    两个进程想要交互需要有一个中间代理
    创建新进程需要对其父进程进行一次克隆
    进程智能操作子进程

    #!/usr/bin/env python3
    # -*- coding:utf-8 -*-
    # -*- Author:Hinimix -*-
    
    import multiprocessing
    import threading
    
    
    def hnm_threa():
        print(threading.get_ident())
    
    def hnm_proce(name):
        print("from multiprocess %s" % name)
        t = threading.Thread(target=hnm_threa)
        t.start()
    
    for tmp in range(10):
        p = multiprocessing.Process(target=hnm_proce, args=(tmp,))
        p.start()
    
    

    进程Queue

    # import multiprocessing
    import threading
    from multiprocessing import Process, Queue  #进程Queue
    def hnm_proce(qq):
    
        qq.put([1, None, "hello"])
    
    q = Queue()
    
    for tmp in range(10):
        p = Process(target=hnm_proce, args=(q,))
        p.start()
    
    print(q.qsize())

     进程Pipe

    from multiprocessing import Process, Pipe 
    import time
    
    
    def hnm_proce(conn):
        conn.send([111, None, "from child 1"])
        conn.send([111, None, "from child 2"])
        time.sleep(.1)
        print("from parent")
        print(conn.recv())
        conn.close()
    
    parent_conn, child_conn = Pipe()
    
    if __name__ == '__main__':
        p = Process(target=hnm_proce, args=(child_conn,))
        p.start()
        print(parent_conn.recv())
        print(parent_conn.recv())
        parent_conn.send("from daddy")
        p.join()

     进程Manager

    from multiprocessing import Process, Manager  
    import os
    
    def f(d, l):
        d[os.getpid()] = os.getpid()
        l.append(os.getpid())
    
    if __name__ == '__main__':
        with Manager() as manager:
            d = manager.dict()      #生成字典,可在多个进程间共享传递
            l = manager.list(range(5))      #生成列表,可在多个进程间共享
            p_list = []
    
            for i in range(10):
                p = Process(target=f, args=(d,l))
                p.start()
                p_list.append(p)
    
            for res in p_list:
                res.join()
    
            print(d)
            print(l)

    进程锁

    from multiprocessing import Process, Lock
    
    
    def func(lock, num):    # 进程锁保证输出完整
        lock.acquire()
        try:
            print("hello %s" % num)
        finally:
            lock.release()
    
    
    if __name__ == '__main__':
        lock = Lock()
    
        for num in range(10):
            Process(target=func, args=(lock, num)).start()

    进程池

    #!/usr/bin/env python3
    # -*- coding:utf-8 -*-
    # -*- Author:Hinimix -*-
    
    # import multiprocessing
    import threading
    from multiprocessing import Process, Pool
    import time
    import os
    
    def Foo(i):
        print("in progress", os.getpid())
        time.sleep(1)
        return i+100
    
    def Bar(arg):
        print("exec done", arg, os.getpid())
    
    pool = Pool(processes=5)
    
    for i in range(10):
        # pool.apply(func=Foo, args=(i,))       #串行同步
        # pool.apply_async(func=Foo, args=(i,))   #并行异步
        pool.apply_async(func=Foo, args=(i,), callback=Bar)   #回调函数
    print("main pid ", os.getpid())
    print("end")
    pool.close()
    pool.join() #如果注释,程序就直接关闭了
  • 相关阅读:
    表格编辑、拖拽、复制、粘贴、剪贴、删除、清空
    winform程序只能打开一个进程
    winform更新程序代码
    winform无标题窗体点击任务栏最小化、还原功能
    winform闪屏问题解决方案
    jQuery 常用方法总结
    理解JSON:3分钟课程
    打造高效的技术团队,我会关注的7个点
    nginx实现网站负载均衡(windows+IIS负载实测)
    怎么用批处理修改host文件
  • 原文地址:https://www.cnblogs.com/hinimix/p/9167511.html
Copyright © 2020-2023  润新知