1. 程式人生 > >Python隨心記--線程列隊

Python隨心記--線程列隊

empty rand move name ati class 一個 消費 spa

import threading,time

li = [1,2,3,4,5,6]
def pri():
    while li:
        a = li[-1]
        print(a)
        time.sleep(1)
        li.remove(a)
        try:
            li.remove(a)
        except Exception as e:
            print(a,e)

t1 = threading.Thread(target=pri,args=())
t1.start()
t2 = threading.Thread(target=pri,args=())
t2.start()
先進先出
import queue   #線程隊列 2.7的時候q為大寫

q = queue.Queue(3)   #FIFO 默認 1先進先出 2先進後出 3後進先出

q.put(12)
q.put(hello)
q.put({name:aaron})
q.put(123,False)   #如果已滿就不再加進去

while 1:
    data = q.get()
    #data = q.get(block = False)

    print(data)
    print(------------------)
先進後出
import queue   #
線程隊列 2.7的時候q為大寫 q = queue.LifoQueue() #FIFO 默認 1先進先出 2先進後出 3後進先出 q.put(12) q.put(hello) q.put({name:aaron}) q.put(123,False) #如果已滿就不再加進去 while 1: data = q.get() print(data) print(------------------) import queue #線程隊列 2.7的時候q為大寫 q = queue.LifoQueue() q.put(12) q.put(
hello) q.put({name:aaron}) q.put(123,False) #如果已滿就不再加進去 print(q.qsize()) print(q.empty()) print(q.full()) print(q.task_done()) #在完成任務之後q.task_done函數向任務已經完成的隊列發送一個信號 print(q.join())
生成消費者模型:通過一個容器來解決生產者和消費者的強耦合問題
import queue,threading,time,random

q = queue.Queue()

def Producter(name):
    count = 0
    while count < 10:
        print(making..............)
        time.sleep(random.randrange(3))
        q.put(count)
        print(Producter %s has producter %s baozi。。 %(name,count))

        count += 1
        print(ok........)

def Consumer(name):
    count = 0

    while count < 10:
        time.sleep(random.randrange(4))

        if not q.empty():
            data = q.get()

            print(data)
            print(\033[32;1mConsumer %s has eat %s baozi..\033[0m %(name,count))
        else:
            print(no baozi anymore)
        count += 1


p = threading.Thread(target=Producter,args=(A,))
c = threading.Thread(target=Consumer,args=(B,))

p.start()
c.start()
存在問題版本
import queue,threading,time,random

q = queue.Queue()

def Producter(name):
    count = 0
    while count < 10:
        print(making..............)
        time.sleep(random.randrange(3))
        q.put(count)
        print(Producter %s has producter %s baozi。。 %(name,count))

        count += 1

        q.task_done()
        #q.join()
        print(ok........)

def Consumer(name):
    count = 0

    while count < 10:
        time.sleep(random.randrange(4))

        if not q.empty():
            data = q.get()
            #q.task_done()
            q.join()
            print(\033[32;1mConsumer %s has eat %s baozi..\033[0m %(name,data))
        else:
            print(no baozi anymore)
        count += 1


p = threading.Thread(target=Producter,args=(A,))
c1 = threading.Thread(target=Consumer,args=(B,))
c2 = threading.Thread(target=Consumer,args=(C,))
c3 = threading.Thread(target=Consumer,args=(D,))

p.start()
c1.start()
c2.start()
c3.start()
解決問題版本
import queue,threading,time,random

q = queue.Queue()

def Producter(name):
    count = 0
    while count < 10:
        print(making..............)
        time.sleep(5)
        q.put(count)
        print(Producter %s has producter %s baozi。。 %(name,count))

        count += 1

        q.task_done()
        #q.join()
        print(ok........)

def Consumer(name):
    count = 0

    while count < 10:
        time.sleep(random.randrange(4))
        print(waiting...........)
        q.join()
        data = q.get()
        #q.task_done()

        print(\033[32;1mConsumer %s has eat %s baozi..\033[0m %(name,data))

        count += 1


p = threading.Thread(target=Producter,args=(A,))
c1 = threading.Thread(target=Consumer,args=(B,))
c2 = threading.Thread(target=Consumer,args=(C,))
c3 = threading.Thread(target=Consumer,args=(D,))

p.start()
c1.start()
c2.start()
c3.start()

import queue,threading,time,random

q = queue.Queue()

def Producter(name):
    count = 0
    while count < 10:
        print(making..............)
        time.sleep(5)
        q.put(count)
        print(Producter %s has producter %s baozi。。 %(name,count))

        count += 1

        # q.task_done()
        q.join()
        print(ok........)

def Consumer(name):
    count = 0

    while count < 10:
        time.sleep(random.randrange(4))

        # q.join()
        data = q.get()
        print(eating...........)
        time.sleep(4)

        q.task_done()

        print(\033[32;1mConsumer %s has eat %s baozi..\033[0m %(name,data))

        count += 1


p = threading.Thread(target=Producter,args=(A,))
c1 = threading.Thread(target=Consumer,args=(B,))
c2 = threading.Thread(target=Consumer,args=(C,))
c3 = threading.Thread(target=Consumer,args=(D,))

p.start()
c1.start()
c2.start()
c3.start()

Python隨心記--線程列隊