Python 互斥锁/可重入锁
Python多线程场景下,线程间的通信可以使用Queue,分为
- FIFO(先入先出)队列Queue: Queue.Queue:队列顺序为先进先出
- LIFO(后入先出)队列LifoQueue: 队列顺序为后进先出
- 优先级队列PriorityQueue:队列会自动根据元素的优先级进行排序,优先级数字越小,代表优先级越高,排在队列的最前面.
1.FIFO(先入先出)队列Queue示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
def work(self):
while True:
try:
item = self.queue.get()
print(f"---work---item:{item}")
self.queue.task_done()
except self.queue.empty():
print(f"queue empty")
time.sleep(2)
with self.lock:
if self.exit_flag:
break
|
2.LIFO(后入先出)队列LifoQueue示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
def lifo_work(self):
while True:
try:
item = self.lifo_queue.get()
print(f"---lifo_work---item:{item}")
self.lifo_queue.task_done()
except self.lifo_queue.empty():
print(f"lifo_queue empty")
time.sleep(2)
with self.lock:
if self.exit_flag:
break
|
3.优先级队列PriorityQueue示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
def priority_work(self):
while True:
try:
item = self.priority_queue.get()
print(f"---priority_queue---item:{item}")
self.priority_queue.task_done()
except self.priority_queue.empty():
print(f"priority_queue empty")
time.sleep(2)
with self.lock:
if self.exit_flag:
break
|
4.其它代码示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
MAX_THREAD_NUM = 3
MAX_QUEUE_NUM = 10
def __init__(self):
self.exit_flag = False
self.lock = threading.Lock()
self.queue = queue.Queue(QueueTest.MAX_QUEUE_NUM)
self.lifo_queue = queue.LifoQueue(QueueTest.MAX_QUEUE_NUM)
self.priority_queue = queue.PriorityQueue(QueueTest.MAX_QUEUE_NUM)
def start(self):
threads = []
print(f"---queue---")
# 先入先出
for i in range(QueueTest.MAX_THREAD_NUM):
t = threading.Thread(target=self.work)
threads.append(t)
t.start()
for i in range(QueueTest.MAX_QUEUE_NUM):
self.queue.put(i)
self.queue.join()
with self.lock:
self.exit_flag = True
for i in range(QueueTest.MAX_THREAD_NUM):
threads[i].join(timeout=10)
threads.clear()
print(f"---lifo_queue---")
# 后入先出
self.exit_flag = False
for i in range(QueueTest.MAX_THREAD_NUM):
t = threading.Thread(target=self.lifo_work)
threads.append(t)
t.start()
for i in range(QueueTest.MAX_QUEUE_NUM):
self.lifo_queue.put(i)
self.lifo_queue.join()
with self.lock:
self.exit_flag = True
for i in range(QueueTest.MAX_THREAD_NUM):
threads[i].join(timeout=10)
threads.clear()
print(f"---priority_queue---")
self.exit_flag = False
for i in range(QueueTest.MAX_THREAD_NUM):
t = threading.Thread(target=self.priority_work)
threads.append(t)
t.start()
for i in range(QueueTest.MAX_QUEUE_NUM):
self.priority_queue.put((QueueTest.MAX_QUEUE_NUM - i, "hello priority_queue" + str(QueueTest.MAX_QUEUE_NUM - i)))
self.priority_queue.join()
with self.lock:
self.exit_flag = True
for i in range(QueueTest.MAX_THREAD_NUM):
threads[i].join(timeout=10)
threads.clear()
print(f"---all finish---")
|
5.执行情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
---queue---
---work---item:0
---work---item:1
---work---item:2
---work---item:4
---work---item:3
---work---item:5
---work---item:6
---work---item:7
---work---item:8
---work---item:9
---lifo_queue---
---lifo_work---item:9
---lifo_work---item:8
---lifo_work---item:7
---lifo_work---item:6
---lifo_work---item:5
---lifo_work---item:4
---lifo_work---item:3
---lifo_work---item:2
---lifo_work---item:1
---lifo_work---item:0
---priority_queue---
---priority_queue---item:(1, 'hello priority_queue1')
---priority_queue---item:(2, 'hello priority_queue2')
---priority_queue---item:(3, 'hello priority_queue3')
---priority_queue---item:(4, 'hello priority_queue4')
---priority_queue---item:(5, 'hello priority_queue5')
---priority_queue---item:(6, 'hello priority_queue6')
---priority_queue---item:(7, 'hello priority_queue7')
---priority_queue---item:(8, 'hello priority_queue8')
---priority_queue---item:(9, 'hello priority_queue9')
---priority_queue---item:(10, 'hello priority_queue10')
---all finish---
|
文章作者
梵梵爸
上次更新
2023-08-23
许可协议
原创文章,如需转载请注明文章作者和出处。谢谢