-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_pool.py
More file actions
130 lines (114 loc) · 10.8 KB
/
Copy pathprocess_pool.py
File metadata and controls
130 lines (114 loc) · 10.8 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import random
import time
from lock import check_lock, file_lock, file_unlock
from create_process import process_create, tasks_create
from time_model import get_now_str, get_time_bystr, get_timedelta_bystr, begin_time
""""
nums = [6, 4, 2, 6, 6, 1, 4, 1, 4, 5, 2, 2, 3, 6, 1, 1, 2, 1, 3, 5, 3, 6, 1, 3, 4, 6, 5, 1, 1, 1, 3, 5, 4, 2, 5, 5, 6, 5, 3, 2, 3, 4, 1, 6]
sleep_time = [2, 3, 2, 1, 2, 1, 1, 2, 1, 3, 1, 3, 1, 1, 2, 1, 1, 1, 3, 2, 3, 3, 2, 3, 2, 2, 1, 2, 3, 2, 3, 3, 3, 3, 2, 2, 2, 1, 3, 2, 2, 3, 2, 1]
tasks = [[1, 4, 4, 2], [1, 5, 5, 3], [3, 5, 4, 4], [2, 3, 2, 3], [3, 4, 1, 3], [1, 4, 2, 3], [2, 1, 5, 7], [2, 4, 4, 4], [2, 2, 4, 4], [2, 1, 4, 1], [2, 2, 3, 3], [3, 2, 3, 10], [3, 2, 4, 8], [2, 2, 2, 9], [3, 4, 4, 4], [1, 5, 2, 3], [2, 5, 5, 9], [2, 1, 2, 5], [2, 2, 5, 8], [3, 3, 3, 7], [1, 2, 5, 5], [3, 4, 4, 4], [2, 1, 3, 2], [2, 5, 4, 1], [3, 2, 3, 1], [3, 3, 1, 1], [3, 4, 3, 7], [2, 1, 4, 9], [1, 4, 4, 5], [2, 5, 1, 8], [3, 2, 5, 5], [3, 3, 5, 9], [2, 3, 3, 7], [3, 3, 4, 6], [1, 1, 1, 8], [3, 1, 3, 10], [1, 5, 5, 7], [3, 3, 5, 6], [3, 1, 2, 1], [3, 4, 2, 10], [1, 3, 3, 1], [2, 2, 4, 9], [2, 3, 3, 4], [2, 3, 5, 7], [2, 5, 1, 4], [1, 1, 1, 3], [1, 1, 5, 1], [3, 3, 3, 6], [2, 4, 1, 9], [1, 3, 2, 2], [1, 5, 3, 5], [1, 1, 2, 4], [1, 1, 4, 5], [1, 2, 2, 3], [2, 3, 4, 2], [2, 3, 2, 3], [3, 4, 4, 4], [1, 2, 1, 10], [2, 3, 3, 9], [1, 4, 3, 5], [2, 1, 4, 2], [3, 3, 3, 8], [2, 3, 2, 9], [3, 5, 5, 9], [2, 2, 3, 8], [2, 2, 1, 9], [3, 2, 3, 7], [1, 2, 5, 6], [1, 3, 3, 5], [1, 1, 4, 10], [3, 2, 4, 4], [3, 3, 5, 4], [1, 3, 4, 8], [1, 3, 2, 2], [3, 4, 3, 3], [2, 4, 1, 10], [1, 3, 3, 5], [3, 2, 4, 9], [3, 3, 4, 3], [1, 3, 3, 3], [2, 5, 2, 9], [2, 5, 3, 8], [2, 3, 5, 2], [1, 3, 1, 9], [1, 2, 3, 8], [3, 5, 4, 2], [3, 2, 5, 6], [1, 1, 5, 4], [3, 5, 2, 4], [1, 3, 3, 2], [3, 5, 5, 5], [2, 5, 3, 1], [3, 5, 2, 6], [3, 5, 2, 2], [2, 4, 4, 1], [2, 4, 5, 9], [1, 5, 1, 8], [1, 2, 5, 8], [3, 4, 3, 1], [1, 4, 2, 6], [3, 4, 3, 7], [1, 1, 5, 4], [3, 1, 3, 9], [1, 3, 1, 7], [3, 5, 5, 1], [2, 5, 1, 3], [3, 4, 3, 5], [2, 4, 1, 9], [1, 2, 5, 10], [2, 1, 2, 10], [2, 4, 4, 9], [3, 2, 2, 1], [2, 5, 4, 7], [1, 1, 4, 8], [1, 5, 3, 4], [1, 5, 4, 2], [3, 4, 3, 7], [2, 4, 3, 3], [3, 5, 4, 1], [3, 5, 4, 9], [3, 2, 4, 3], [1, 3, 2, 2], [1, 3, 3, 2], [1, 5, 4, 5], [2, 5, 1, 5], [1, 4, 5, 2], [2, 4, 5, 10], [3, 4, 2, 8], [3, 1, 3, 5], [1, 2, 5, 8], [3, 2, 2, 7], [1, 1, 1, 5], [3, 3, 4, 3], [3, 4, 3, 7], [3, 1, 1, 7], [1, 5, 3, 6], [2, 4, 1, 2], [2, 2, 3, 9], [2, 2, 1, 9], [3, 2, 4, 10], [2, 3, 4, 1], [2, 5, 4, 8], [3, 4, 2, 4], [1, 3, 2, 5], [3, 2, 3, 8], [1, 3, 2, 1], [3, 4, 1, 8], [2, 4, 1, 9], [2, 2, 1, 9], [3, 1, 4, 8]]
"""
"""
nums = [1, 3, 2, 2, 2, 5, 6, 5, 6, 3, 4, 6, 3, 5, 2, 5, 3, 1, 3, 4, 1, 4, 6, 1, 5, 4, 2, 3, 1, 3, 6, 3, 6, 1, 3, 6, 1, 3, 5, 4, 5, 5, 3]
sleep_time = [1, 1, 3, 2, 2, 1, 1, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 3, 2, 3, 3, 3, 3, 2, 2, 2, 3, 3, 2, 2, 2, 2, 2, 3]
tasks = [[3, 11, 21, 8], [2, 15, 21, 1], [2, 11, 7, 7], [3, 19, 16, 1], [1, 21, 24, 4], [2, 17, 6, 8], [3, 21, 18, 18], [1, 20, 18, 4], [2, 18, 18, 1], [1, 9, 25, 6], [3, 5, 17, 3], [3, 7, 24, 10], [2, 18, 13, 13], [3, 11, 13, 10], [1, 18, 22, 14], [3, 22, 9, 2], [2, 21, 17, 19], [2, 17, 20, 7], [1, 21, 10, 11], [3, 7, 25, 15], [3, 16, 14, 14], [2, 18, 25, 8], [2, 8, 10, 9], [1, 15, 12, 6], [1, 13, 15, 13], [2, 16, 6, 1], [3, 7, 23, 15], [1, 8, 24, 2], [1, 16, 20, 13], [3, 5, 12, 6], [3, 19, 9, 9], [2, 20, 7, 6], [3, 21, 19, 5], [1, 12, 15, 13], [1, 22, 21, 20], [1, 18, 18, 17], [2, 9, 19, 5], [2, 19, 7, 13], [2, 11, 12, 3], [3, 20, 21, 6], [3, 6, 10, 4], [3, 10, 11, 3], [3, 15, 8, 7], [3, 8, 6, 7], [1, 14, 5, 3], [2, 21, 15, 4], [3, 24, 9, 2], [1, 8, 20, 6], [2, 10, 14, 6], [1, 15, 10, 10], [1, 19, 5, 11], [3, 19, 9, 9], [3, 20, 5, 4], [2, 21, 8, 4], [3, 10, 19, 6], [2, 7, 9, 2], [3, 7, 21, 13], [2, 14, 7, 8], [3, 11, 16, 1], [3, 11, 9, 3], [1, 25, 14, 6], [3, 6, 5, 1], [1, 23, 9, 3], [3, 10, 15, 10], [3, 24, 16, 20], [1, 18, 12, 15], [3, 20, 8, 6], [1, 12, 18, 2], [1, 7, 23, 12], [1, 10, 16, 4], [1, 8, 6, 5], [1, 24, 23, 20], [2, 11, 21, 6], [1, 25, 24, 5], [2, 11, 13, 6], [2, 10, 24, 14], [2, 21, 12, 1], [2, 11, 24, 9], [2, 8, 8, 8], [2, 8, 20, 11], [2, 25, 11, 5], [1, 16, 24, 9], [1, 17, 12, 10], [3, 12, 7, 8], [3, 21, 25, 2], [3, 21, 8, 1], [2, 18, 24, 8], [1, 17, 24, 10], [2, 19, 13, 2], [1, 12, 15, 12], [3, 13, 20, 9], [2, 21, 17, 12], [3, 7, 13, 8], [3, 19, 8, 10], [1, 6, 9, 3], [2, 23, 14, 10], [3, 18, 14, 3], [1, 7, 17, 1], [1, 7, 23, 13], [1, 6, 16, 3], [2, 21, 7, 6], [1, 20, 20, 11], [3, 24, 25, 21], [1, 9, 20, 10], [2, 6, 20, 11], [3, 12, 12, 5], [1, 19, 14, 4], [1, 18, 10, 8], [1, 23, 5, 11], [2, 15, 18, 16], [1, 9, 22, 13], [2, 15, 9, 8], [3, 15, 15, 2], [1, 10, 17, 1], [3, 8, 15, 5], [1, 20, 9, 4], [1, 11, 15, 9], [3, 12, 16, 3], [1, 25, 15, 1], [3, 23, 23, 5], [1, 8, 9, 5], [3, 7, 11, 9], [3, 7, 20, 1], [2, 21, 10, 11], [2, 20, 18, 9], [2, 25, 23, 6], [2, 8, 15, 9], [3, 11, 17, 14], [2, 19, 22, 18], [1, 22, 23, 7], [3, 6, 25, 2], [2, 24, 14, 14], [3, 19, 8, 4], [3, 25, 6, 9], [3, 10, 9, 6], [1, 6, 21, 3], [3, 22, 16, 14], [2, 20, 22, 5], [2, 7, 24, 9], [2, 17, 17, 13], [2, 15, 7, 1], [2, 23, 11, 12], [3, 10, 18, 10], [2, 13, 13, 5], [1, 13, 22, 14], [2, 20, 11, 3], [1, 25, 22, 6], [3, 12, 24, 9], [1, 22, 25, 16], [3, 20, 17, 3], [2, 19, 21, 19], [2, 17, 12, 12]]
"""
nums = [3, 2, 4, 4, 3, 2, 4, 5, 3, 5, 5, 5, 6, 6, 6, 2, 2, 3, 4, 5, 5, 5, 2, 5, 3, 3, 6, 5, 2, 2, 4, 3, 2, 4, 3, 4, 4, 2, 6, 4]
sleep_time = [2, 1, 1, 1, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 2, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 2, 2, 1, 1, 1]
tasks = [[3, 19, 20, 14], [1, 21, 5, 13], [2, 21, 8, 14], [1, 24, 6, 7], [2, 25, 22, 18], [3, 18, 24, 15], [1, 8, 18, 8], [2, 7, 13, 4], [1, 14, 19, 8], [3, 5, 11, 8], [3, 6, 11, 8], [3, 24, 11, 7], [1, 15, 15, 15], [3, 17, 6, 9], [2, 18, 13, 7], [3, 16, 22, 18], [2, 22, 19, 10], [3, 12, 15, 10], [3, 11, 7, 7], [3, 21, 24, 11], [1, 15, 18, 15], [3, 22, 10, 14], [1, 23, 21, 16], [1, 12, 21, 9], [2, 15, 25, 17], [3, 19, 13, 15], [2, 9, 19, 5], [3, 9, 7, 3], [1, 11, 24, 10], [1, 10, 25, 16], [2, 10, 12, 9], [3, 11, 9, 6], [1, 18, 19, 16], [1, 25, 13, 15], [3, 5, 10, 5], [2, 18, 16, 11], [3, 8, 25, 15], [2, 21, 18, 10], [3, 21, 13, 10], [1, 9, 12, 10], [3, 14, 18, 9], [2, 13, 15, 13], [3, 19, 9, 6], [2, 21, 18, 8], [1, 25, 8, 10], [2, 8, 6, 7], [3, 7, 20, 6], [1, 12, 16, 12], [3, 20, 5, 9], [3, 5, 10, 7], [2, 21, 15, 10], [2, 22, 14, 17], [1, 19, 19, 16], [1, 14, 21, 16], [3, 24, 9, 11], [2, 16, 25, 15], [2, 25, 12, 7], [3, 14, 21, 8], [2, 12, 21, 8], [2, 22, 19, 11], [2, 16, 9, 7], [3, 20, 19, 18], [2, 15, 14, 12], [3, 14, 9, 10], [2, 8, 8, 8], [1, 19, 24, 15], [3, 7, 22, 8], [2, 11, 10, 5], [1, 8, 9, 4], [3, 24, 24, 12], [1, 19, 22, 20], [3, 22, 19, 20], [1, 25, 13, 14], [3, 18, 6, 4], [3, 7, 17, 9], [1, 8, 18, 13], [3, 11, 13, 8], [2, 13, 12, 6], [2, 11, 15, 8], [2, 7, 18, 5], [1, 20, 24, 16], [2, 12, 22, 13], [1, 5, 21, 11], [1, 16, 5, 4], [1, 17, 20, 13], [1, 20, 25, 10], [2, 20, 17, 11], [3, 9, 21, 14], [3, 22, 22, 18], [2, 21, 22, 20], [2, 23, 13, 7], [1, 24, 8, 11], [2, 17, 6, 11], [2, 7, 7, 4], [1, 13, 22, 9], [2, 21, 18, 11], [2, 19, 6, 8], [1, 16, 24, 14], [3, 23, 16, 19], [1, 9, 22, 12], [1, 18, 7, 10], [2, 15, 23, 12], [2, 7, 8, 7], [3, 22, 20, 15], [1, 9, 9, 9], [3, 10, 15, 5], [1, 8, 13, 8], [1, 13, 16, 11], [1, 20, 22, 15], [1, 22, 23, 19], [3, 17, 19, 8], [2, 7, 21, 7], [3, 24, 23, 15], [3, 13, 13, 11], [1, 24, 8, 13], [2, 20, 9, 9], [3, 15, 21, 16], [2, 13, 8, 6], [3, 25, 14, 18], [1, 14, 6, 4], [3, 5, 13, 9], [1, 17, 9, 12], [2, 19, 11, 6], [3, 14, 21, 17], [2, 23, 19, 14], [1, 8, 13, 10], [2, 17, 23, 9], [3, 15, 8, 4], [2, 9, 21, 11], [1, 23, 24, 9], [2, 24, 25, 17], [3, 8, 15, 4], [1, 16, 20, 18], [3, 9, 12, 5], [2, 13, 15, 6], [3, 17, 21, 12], [2, 9, 17, 12], [1, 19, 15, 15], [1, 19, 10, 11], [1, 23, 21, 8], [1, 5, 18, 7], [3, 12, 16, 11], [1, 19, 8, 5], [1, 5, 16, 4], [1, 17, 5, 10], [2, 17, 15, 6], [2, 14, 20, 17], [3, 20, 9, 7], [1, 7, 16, 9], [2, 6, 19, 5], [3, 25, 7, 10], [1, 7, 14, 4], [2, 19, 14, 9]]
pid = 1
process_pool_1 = [] # 进程池level_1
process_pool_2 = [] # 进程池level_2
process_pool_3 = [] # 进程池level_3
def switch_level(level, pool_1, pool_2, pool_3, process):
if level == 1:
pool_1.append(process)
"""
try:
with open('pool_1.json', 'a+', encoding='utf-8') as fs:
json.dump(process, fs)
except IOError as e:
print(e)
"""
while check_lock("pool_1.txt", "pool_1_lock.txt"):
time.sleep(0.05)
file_lock("pool_1.txt", "pool_1_lock.txt")
f = open("pool_1.txt", "a")
for line in process:
f.write(str(line) + ' ')
f.write('\n')
f.close()
file_unlock("pool_1.txt", "pool_1_lock.txt")
return pool_1
elif level == 2:
pool_2.append(process)
"""
try:
with open('pool_1.json', 'a+', encoding='utf-8') as fs:
json.dump(process, fs)
except IOError as e:
print(e)
"""
while check_lock("pool_2.txt", "pool_2_lock.txt"):
time.sleep(0.05)
file_lock("pool_2.txt", "pool_2_lock.txt")
f = open("pool_2.txt", "a")
for line in process:
f.write(str(line) + ' ')
f.write('\n')
f.close()
file_unlock("pool_2.txt", "pool_2_lock.txt")
return pool_2
elif level == 3:
pool_3.append(process)
""""
try:
with open('pool_1.json', 'a+', encoding='utf-8') as fs:
json.dump(process, fs)
except IOError as e:
print(e)
"""
while check_lock("pool_3.txt", "pool_3_lock.txt"):
time.sleep(0.05)
file_lock("pool_3.txt", "pool_3_lock.txt")
f = open("pool_3.txt", "a")
for line in process:
f.write(str(line) + ' ')
f.write('\n')
f.close()
file_unlock("pool_3.txt", "pool_3_lock.txt")
return pool_3
# begin_time = get_now_str()
# current_time = 0
# 创建20个进程并加入进程池
"""
i = 3
while i > 0:
# while 1:
sleep = random.randint(1, 5) # 随机等待一段时间
for j in range(sleep):
time.sleep(1)
# temp = process_create(pid, current_time) # 创建一个进程
# temp = process_create(pid, get_timedelta_bystr(begin_time, get_now_str()))
temp = process_create(pid, get_now_str())
switch_level(temp[1], process_pool_1, process_pool_2, process_pool_3, temp) # 放入对应进程池
print("生成任务"+str(pid)+"成功")
i -= 1
pid += 1
"""
i = 0
t = 0
n = 0
while i < len(tasks):
sleep = sleep_time[t]
t += 1
for j in range(sleep):
time.sleep(1)
num = nums[n]
n += 1
for k in range(num):
temp = tasks_create(pid, get_now_str(), tasks[i])
switch_level(temp[1], process_pool_1, process_pool_2, process_pool_3, temp)
print("生成任务" + str(pid) + "成功")
i += 1
pid += 1