-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleThreads.py
More file actions
40 lines (31 loc) · 1.06 KB
/
SimpleThreads.py
File metadata and controls
40 lines (31 loc) · 1.06 KB
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
from queue import Queue
from threading import Thread
exqueue = Queue(1) # Size 1 queue box, let's change 1 ~ 40
def consumer():
while True:
print("[Consumer] I'm Ready")
woker = exqueue.get()
print("[Consumer] Working")
# Some Process Real todo
# ~~~
exqueue.task_done() # If task_done(), producer does not have to queue or poll consuming threads with joins.
print("[Consumer] Processing Done")
thread = Thread(target=consumer).start()
exqueue.put(object())
print("[Producer] Put into Queue 1st")
exqueue.put(object())
print("[Producer] Put into Queue 2nd")
exqueue.join()
print("[Producer] stand by")
#As is obvious, if the queue is empty, the join method of queue does not complete until all tasks have been added to the queue call task_done.
# Expected Results
# [Producer] Put into Queue 1st
# [Consumer] I'm Ready
# [Consumer] Working
# [Consumer] Processing Done
# [Consumer] I'm Ready
# [Producer] Put into Queue 2nd
# [Consumer] Working
# [Consumer] Processing Done
# [Consumer] I'm Ready
# [Producer] stand by