forked from hemuAiopen/pythonclass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreaddemon.py
More file actions
46 lines (35 loc) · 871 Bytes
/
threaddemon.py
File metadata and controls
46 lines (35 loc) · 871 Bytes
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
import threading
import time
total = 4
def create_items_1():
global total
for i in range(10):
time.sleep(2)
print('creator1 add item')
total += 1
print("create_item_1 is done")
def create_items_2():
global total
for i in range(7):
time.sleep(2)
print('creator2 add item')
total += 1
print("create_item_2 is done")
def limit_items():
global total
while True:
if total > 5:
print('overload')
total -= 3
print('subtracted 3')
else:
time.sleep(1)
print('waiting')
creator1 = threading.Thread(target=create_items_1)
creator2 = threading.Thread(target=create_items_2)
limitor = threading.Thread(target=limit_items, daemon=True)
creator1.start()
creator2.start()
limitor.start()
creator1.join()
creator2.join()