多线程利器(queue)

queue is especially usefull in threaded programming when in formation must be exchanged safely between multiple threads.

queue定义了3种信息队列模式类

Queue([maxsize]):FIFO列队模式。[maxsize]定义列队容量,缺省0,即无穷大;

LifoQueue([maxsize]):LIFO列队模式

PriorityQueue([maxsize]):优先级列队模式,使用此列队时,项目应该是(priority,data)的形式。

queue队列类的方法

q.put(item[,block,[timeout]]):将item放入队列,如果block设置为True(默认)时,队列满了,调用者将被阻塞,否则抛出Full异常,timeout设置阻塞超时;

q.get([block,[timeout]]):从队列中取出一个项目;

q.qsize():返回队列大小

q.full():如果队列满了返回True,否则返回False;

q.empty():如果队列为空返回True,否则为False;

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import queue

d = queue.Queue()

d.put('jinxin')
d.put('xiaohu')
d.put('haoran')

print(d.get())

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py
jinxin

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import queue

d = queue.Queue()

d.put('jinxin')
d.put('xiaohu')
d.put('haoran')

print(d.get())
print(d.get())

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py
jinxin
xiaohu

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import queue

d = queue.Queue(3)

d.put('jinxin')
d.put('xiaohu')
d.put('haoran')

print(d.get())
print(d.get())

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py
jinxin
xiaohu

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import queue

d = queue.Queue(2)

d.put('jinxin')
d.put('xiaohu')
d.put('haoran')

print(d.get())
print(d.get())

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import queue

d = queue.Queue(2)

d.put('jinxin')
d.put('xiaohu')
d.put('haoran',0)

print(d.get())
print(d.get())

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py
Traceback (most recent call last):
  File "/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py", line 13, in <module>
    d.put('haoran',0)
  File "/usr/lib/python3.8/queue.py", line 136, in put
    raise Full
queue.Full

Process finished with exit code 1

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import queue

d = queue.Queue(2)

d.put('jinxin')
d.put('xiaohu')
d.put('haoran',1)

print(d.get())
print(d.get())

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import queue

d = queue.Queue(2)

d.put('jinxin',0)
d.put('xiaohu')
d.put('haoran')

print(d.get())
print(d.get())

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import queue

d = queue.Queue(3)

d.put('jinxin',0)
d.put('xiaohu')
d.put('haoran')

print(d.get())
print(d.get())
print(d.get())

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py
jinxin
xiaohu
haoran

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import queue

d = queue.Queue(3)

d.put('jinxin',0)
d.put('xiaohu')
d.put('haoran')

print(d.get())
print(d.get())
print(d.get())
print(d.get())

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py
jinxin
xiaohu
haoran

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import queue

d = queue.Queue(3)

d.put('jinxin',0)
d.put('xiaohu')
d.put('haoran')

print(d.get())
print(d.get())
print(d.get())
print(d.get(0))

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py
Traceback (most recent call last):
  File "/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py", line 18, in <module>
    print(d.get(0))
  File "/usr/lib/python3.8/queue.py", line 167, in get
    raise Empty
_queue.Empty
jinxin
xiaohu
haoran

Process finished with exit code 1

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import queue    #队列模块

d=queue.Queue(3) #创建队列对象,后面可以设置数据长度。数据个数不够,会阻塞,默认为0无限大,只要小于0就无限大

d.put('jinxin',0) #像队列插入数据
d.put('xiaohu')
d.put('haoran',0)   #后面数字默认为1为阻塞,0代表报错,queue.Full

print(d.get())  #拿数据,先进先出,FIFO。
print(d.get())
print(d.get())
#print(d.get(0))  #没有数据空,也会阻塞,等待插入,默认为True阻塞,timeout超时。queue.Empty

d.put(1)

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py
jinxin
xiaohu
haoran

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import threading,time

li=[1,2,3,4,5]

def pri():
    while li:
        a=li[-1]
        print(a)
        time.sleep(1)
        try:
            li.remove(a)
        except:
            print('----',a)

t1 = threading.Thread(target=pri,args=())
t1.start()
t2 = threading.Thread(target=pri,args=())
t2.start()

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py
5
5
4
---- 5
4
3
---- 4
3
2---- 3
2

1---- 2

1
---- 1

Process finished with exit code 0

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import threading,queue
from time import sleep
from random import randint
class Production(threading.Thread):
    def run(self):
        while True:
            r = randint(0,100)
            q.put(r)
            print("生产出来%s号包子" %r)
            sleep(1)

class Proces(threading.Thread):
    def run(self):
        while True:
            re = q.get()
            print("吃掉%s号包子" %re)

if __name__ == "__main__":
    q = queue.Queue(10)
    threads = [Production(),Production(),Production(),Proces()]
    for t in threads:
        t.start()

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py
生产出来51号包子
生产出来27号包子
生产出来42号包子
吃掉51号包子
吃掉27号包子
吃掉42号包子
生产出来32号包子吃掉32号包子

生产出来62号包子
生产出来66号包子
吃掉62号包子
吃掉66号包子
生产出来61号包子吃掉61号包子

生产出来1号包子吃掉1号包子生产出来31号包子
吃掉31号包子

#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: queue
# time: 2021/04/06

import threading,queue
from time import sleep
from random import randint
class Production(threading.Thread):
    def run(self):
        while True:
            r = randint(0,100)
            q.put(r)
            print(q.qsize())
            print("生产出来%s号包子" %r)
            sleep(1)

class Proces(threading.Thread):
    def run(self):
        while True:
            re = q.get()
            print("吃掉%s号包子" %re)

if __name__ == "__main__":
    q = queue.Queue(10)
    threads = [Production(),Production(),Production(),Proces()]
    for t in threads:
        t.start()

/home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/queuing.py
1
生产出来55号包子
2
生产出来99号包子
3
生产出来43号包子
吃掉55号包子
吃掉99号包子
吃掉43号包子
1吃掉41号包子
1

生产出来49号包子
生产出来41号包子吃掉49号包子