zoukankan      html  css  js  c++  java
  • 进程与线程

    from multiprocessing import Queue
    
    q = Queue(5) # 设置队列的长度
    
    q.put(1) # 往队列中写入数据
    q.put(2)
    q.put(3)
    q.put(4)
    q.put(5)  
    # print(q.full())  # 判断当前队列是否已满
    print(q.get())
    print(q.get()) # 从队列中读取数据
    print(q.get())
    print(q.get())
    print(q.get())
    print(q.get())
    # print(q.empty())  # 判断当前队列是否为空
    # print(q.get_nowait()) # 从队列中读取数据,若队列为空,则报错
    进程间通信
    from multiprocessing import Process,Queue
    
    def producer(q):
        q.put('xx')
    
    def consumer(q):
        print(q.get())
    
    
    
    if __name__ == '__main__':
        q = Queue()
        p = Process(target=producer,args=(q,))
        c = Process(target=consumer,args=(q,))
        p.start()
        c.start()
    进程间通信实操
    from multiprocessing import JoinableQueue,Process
    import time
    
    # 往队列写入数据
    def producer(name,food,q): 
        for i in range(5):
            data = f'{name}生产了{food}'
            time.sleep(1)
            q.put(data)
    
    # 从队列读取数据
    def consumer(name,q):
        while True:
            data = q.get()
            if data == None:break
            print(f'{name}吃了{data}')
            q.task_done()
    
    if __name__ == '__main__':
        q = JoinableQueue()
    
        p = Process(target=producer,args=('egon','chess',q))
        p1 = Process(target=producer,args=('tank','burger',q))
    
        c = Process(target=consumer,args=('test1',q))
        c1 = Process(target=consumer,args=('test2',q))
    
        p.start()
        p1.start()
    
        c.daemon = True
        c1.daemon = True
    
        c.start()
        c1.start()
    
        p.join()
        p1.join()
    
        q.join()
    线程中的生产者与消费者模型
    from threading import Thread
    
    money = 666
    
    
    def task():
        global money
        money = 999
    
    t = Thread(target=task)
    t.start()
    print(money)
    线程间的通信
    from threading import Thread,current_thread
    import time
    
    def task(i):
        print(current_thread().name)
        time.sleep(i)
        print('GG')
    
    t = Thread(target=task,args=(2,))
    t.daemon = True
    
    t.start()
    print('main')
    守护线程
    方式一:调用Thread类
    from threading import Thread
    import time
    def task(name):
        print(f'{name} is running')
        time.sleep(2)
        print(f'{name} is over')
    
    
    t = Thread(target=task,args=('egon',))
    t.start()
    print('main')
    
    方式二:继承Thread类
    
    from threading import Thread
    import time
    
    class MyThread(Thread):
        def __init__(self,name):
            super().__init__()
            self.name = name
    
        def run(self):
            print(f'{self.name} is running')
            time.sleep(3)
            print(f'{self.name} is over')
    
    t = MyThread('egon')
    t.start()
    print("main")
    创建线程的两种方式
    from threading import Thread,Lock
    import time
    
    n = 10
    
    
    def task(mutex):
        global n
    
        mutex.acquire()
        tmp = n
        time.sleep(1)
        n = tmp - 1
        mutex.release()
        print(n)
    
    t_list = []
    mutex = Lock()
    
    for i in range(10):
        t = Thread(target=task,args=(mutex,))
        t.start()
        t_list.append(t)
    # print(t_list)
    for t in t_list:
        t.join()
    print(n)
    互斥锁

      线程和进程的方法类似,但是也存在区别:

      1:若把进程比作车间,那进程就相当于流水线.也就是说,进程提供资源,线程执行任务.

      2:进程与进程间,数据是隔离的.而线程与线程间,数据是共享的.

      

  • 相关阅读:
    实现多页签切换效果
    CSS样式display:none和visibility:hidden的区别
    canvas主要属性和方法
    Web前端的35个jQuery小技巧
    div+css3实现的小丸子和爷爷
    Jquery实现手机上下滑屏滑动的特效代码
    使用phantomjs生成网站快照
    VSCode配置Go language tools
    TypeScript中慎用forEach
    win8开发之数据绑定控件Gridview以分组及不同项模板的形式呈现数据
  • 原文地址:https://www.cnblogs.com/hellozizi/p/11342501.html
Copyright © 2011-2022 走看看