• 多线程 同步对象 event 简单实例 &进程间通信


    多线程 同步对象event

    import threading,time
    class Boss(threading.Thread):
    
        def run(self):
            print("BOSS:今晚大家都要加班到22:00。")
            print(event.isSet())# False
            #设置之后 worker才可以往下执行
            event.set()
            time.sleep(5)
            print("BOSS:<22:00>可以下班了。")
            print(event.isSet())
            event.set()
    
    class Worker(threading.Thread):
        def run(self):
            #到这里先不往下执行,等到Boss线程 set之后
            event.wait()#    一旦event被设定,等同于pass
    
            print("Worker:哎……命苦啊!")
            time.sleep(1)
            #清空状态,然后在设置,等待boss再次设置
            event.clear()
            event.wait()
            print("Worker:OhYeah!")
    
    
    if __name__=="__main__":
        event=threading.Event()
    
    
        threads=[]
        for i in range(5):
            threads.append(Worker())
        threads.append(Boss())
        for t in threads:
            t.start()
        for t in threads:
            t.join()
    
        print("ending.....")

    进程间通信

    import queue,time
    
    import multiprocessing
    def foo(q):
        time.sleep(1)
        print("son process",id(q))
        q.put(123)
        q.put("yuan")
    
    if __name__ == '__main__':
        #q=queue.Queue()
        q=multiprocessing.Queue()
        p=multiprocessing.Process(target=foo,args=(q,))
        p.start()
        #p.join()
        print("main process",id(q))
        print(q.get())
        print(q.get())
    
    
    
    
    
    
    from multiprocessing import Process, Pipe
    def f(conn):
        conn.send([12, {"name":"yuan"}, 'hello'])
        response=conn.recv()
        print("response",response)
        conn.close()
        print("q_ID2:",id(conn))
    
    if __name__ == '__main__':
    
        parent_conn, child_conn = Pipe() #双向管道
    
        print("q_ID1:",id(child_conn))
        p = Process(target=f, args=(child_conn,))
        p.start()
    
        print(parent_conn.recv())   # prints "[42, None, 'hello']"
        parent_conn.send("儿子你好!")
        p.join()
    
    
    from multiprocessing import Process, Manager
    
    def f(d, l,n):
    
        d[n] = '1'    #{0:"1"}
        d['2'] = 2    #{0:"1","2":2}
    
        l.append(n)    #[0,1,2,3,4,   0,1,2,3,4,5,6,7,8,9]
        #print(l)
    
    
    if __name__ == '__main__':
    
        with Manager() as manager:
    
            d = manager.dict()#{}
    
            l = manager.list(range(5))#[0,1,2,3,4]
    
    
            p_list = []
    
            for i in range(10):
                p = Process(target=f, args=(d,l,i))
                p.start()
                p_list.append(p)
    
            for res in p_list:
                res.join()
    
            print(d)
            print(l)
  • 相关阅读:
    sqlserver 中的 substring函数(转)
    C#二个相减怎么获得天数,就是比如201225 与201231之间相差的天数
    C++文件添加到项目中
    VS2008动态链接库(DLL)的创建与导入
    美剧字幕绿箭侠第1季第7集
    C++中#define用法
    C++头文件的重复引用
    visual studio中解决方案是什么
    NewWords/300400
    指针
  • 原文地址:https://www.cnblogs.com/xiaoshi657/p/9033912.html
Copyright © 2020-2023  润新知