Python随心记--线程列队

时间:2021-12-29 23:58:50
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()