-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground_task.py
More file actions
executable file
·342 lines (257 loc) · 9.17 KB
/
background_task.py
File metadata and controls
executable file
·342 lines (257 loc) · 9.17 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# 用于后台运行,以提供更好的用户交互
import subprocess
import time
import uuid
from PyQt5.QtCore import QThread, pyqtSignal
import os
import requests
def getUid():
uid = uuid.uuid4()
return uid
class monitorThread(QThread):
"""For demultiplex"""
finished = pyqtSignal(str)
updated = pyqtSignal(int)
uid = ""
def __init__(self,uid = "sdf"):
self.uid = uid
super(monitorThread,self).__init__()
def monitor(self):
'''统计完成的文件数,从而获取进度'''
self.task_count_path = "/tmp/" + self.uid
path = self.task_count_path
try:
finnished_task = os.listdir(path)
finnished_num = len(finnished_task)
except:
return 0
return finnished_num
def run(self) -> None:
finnished_sum = 0
print("sdfsdafdsaf " + self.uid)
while True:
current_sum = int(self.monitor())
if finnished_sum != current_sum:
finnished_sum = current_sum
self.updated.emit(finnished_sum)
print(finnished_sum)
else:
print("on going..." + str(current_sum))
time.sleep(0.5)
class bgThread(QThread):
finished = pyqtSignal(str)
updated = pyqtSignal(int)
uid = ""
def __init__(self,uid = "sdf"):
self.uid = uid
super(bgThread,self).__init__()
def bgTask(self):
os.system("bash ./.run.sh " + str(self.uid))
import time
# time.sleep(20)
self.finished.emit("done")
def run(self) -> None:
self.bgTask()
class bgCRISPResso(QThread):
finished = pyqtSignal(str)
updated = pyqtSignal(int)
uid = ""
def __init__(self,uid = "sdf",cmdList = []):
self.uid = uid
self.cmd_list = cmdList
super(bgCRISPResso,self).__init__()
def bgTask(self):
header = "#!/bin/bash\n source ~/miniconda3/bin/activate NGS \n" # bash文件头
authorInfo = """# This Script is generated automatically. Do not modify anything unless you know what you are doing.
# Script Author:\tMo Qiqin
# Contact:\tmoqq@shanghaitech.edu.cn\n
uid=$1
"""
bashData = [header,authorInfo]
# bashData0 = list(bashData)
max_thread = int(os.cpu_count()) * 2
counter = 0
# i=0
for task in self.cmd_list:
# i=i+1
CMD = "{\n" + task + " \n}&\n" + "\n clear \n"
bashData.append(CMD)
counter = counter + 1
if counter == max_thread:
bashData.append("\nwait\n")
counter = 0
a = "".join(bashData)
with open(".run.sh", "w") as f:
f.write(a)
os.system("bash ./.run.sh" )
# 运行结束删除内容
bashData = [header,authorInfo]
os.system('rm -rf ./.run.sh')
self.updated.emit(max_thread)
# for 循环结束但是没有完成最后的剩余任务:
bashData.append("\nwait\n")
a = "".join(bashData)
with open(".run.sh", "w") as f:
f.write(a)
os.system("bash ./.run.sh" )
os.system("rm -rf ./.run.sh")
self.finished.emit("done")
def run(self) -> None:
self.bgTask()
class bgCRISPResso2(QThread):
finished = pyqtSignal(str)
updated = pyqtSignal(int)
uid = ""
def __init__(self, uid="sdf", cmdList=[]):
self.uid = uid
self.cmd_list = cmdList
self.waitting = self.cmd_list.copy()
self.running = []
self.finished_tasks =[]
super(bgCRISPResso2, self).__init__()
def update(self,i):
self.running.remove(i)
self.waitting.remove(i)
self.finished_tasks.append(i)
self.updated.emit(1)
def terminate(self) -> None:
self.waitting = []
self.running = []
self.cmd_list = []
for i in self.tasks:
i.terminate()
super(bgCRISPResso2, self).terminate()
def freeMem(self):
try:
with open('/proc/meminfo') as fd:
for line in fd:
if line.startswith('MemTotal'):
total = line.split()[1]
continue
if line.startswith('MemFree'):
free = line.split()[1]
break
FreeMem = int(free) / 1024.0
TotalMem = int(total) / 1024.0
# print("FreeMem:" + "%.2f" % FreeMem + 'M')
#
# print("TotalMem:" + "%.2f" % TotalMem + 'M')
#
# print("FreeMem/TotalMem:" + "%.2f" % ((FreeMem / TotalMem) * 100) + '%')
free = (FreeMem / TotalMem)
except:
print("无法获取内存使用信息!")
free = 0.8
return free
def bgTask(self):
# bashData0 = list(bashData)
max_thread = int(os.cpu_count())
# max_thread = 80
self.tasks = []
while True:
# 判断队列是否满了
if len(self.running) <= max_thread:
for i in self.cmd_list:
# 判断任务是否已经运行过了
if i in self.running:
pass
elif i in self.finished_tasks:
pass
else: # 任务未运行过
#判断内存没爆炸
# free_memory = self.freeMem()
# if free_memory < 0.15:
# print("内存将满,暂停添加任务")
# time.sleep(5)
# else:
# print("剩余内存/总内存 = " + "%.2f" % (free_memory * 100) + '%')
if len(self.running) <= max_thread:
locals()["task_"+str(i)] = bgRun(i)
self.running.append(i)
locals()["task_" + str(i)].start()
locals()["task_" + str(i)].finished.connect(self.update)
self.tasks.append(locals()["task_"+str(i)])
time.sleep(0.1)
if len(self.waitting) == 0:
self.finished.emit('')
return
def run(self) -> None:
self.bgTask()
class bgRun(QThread):
started = pyqtSignal(str)
finished = pyqtSignal(str)
def __init__(self,cmd="sdf"):
self.cmd = cmd
super(bgRun, self).__init__()
def run(self) -> None:
# self.started.emit(self.cmd)
os.system("~/miniconda3/bin/conda run -n NGS " + self.cmd)
print("~/miniconda3/bin/conda run -n NGS " + self.cmd)
self.finished.emit(self.cmd)
class lyricThread(QThread):
updated = pyqtSignal(str)
def __init__(self):
super(lyricThread,self).__init__()
def getLyric(self):
try:
url2 = 'https://v1.jinrishici.com/all'
lyric = requests.get(url2, timeout=1).json()
content = lyric['content']
try:
origin = lyric['origin']
except:
origin = "Unknown"
try:
author = lyric['author']
except:
author = "Unknown"
output = content + "\n\t\t\t" + "——《" + origin + "》\t" + author
return output
except Exception as e:
print(e)
output = "扩增子测序分析" + "\n\t\t\t" + "——Written by M.Q. at ShanghaiTech University"
return output
def run(self) -> None:
while True:
time.sleep(600)
lyric = self.getLyric()
self.updated.emit(lyric)
class sleepTask(QThread):
finished = pyqtSignal(str)
def __init__(self, id = ""):
self.id = str(id)
super(sleepTask, self).__init__()
def run(self) -> None:
print("start sleep " + self.id)
# time.sleep(1)
print("sleep compleat " + self.id)
os.system("touch /home/chief/2")
class bcl2fastqThread(QThread):
finished = pyqtSignal(str)
def __init__(self):
super(bcl2fastqThread,self).__init__()
def bcl2fastq(self):
log_path = "/tmp/bcl2fastq_" + str(time.time())
os.system("bash ./.run.sh 2>" +log_path)
# info = subprocess.Popen(["bash ./.run.sh"], shell=True, stderr=subprocess.PIPE)
# info = str(info.stderr.read())
# print(info)
try:
with open(log_path, "r") as f:
log = f.read()
except Exception as e:
print(e)
log_last = log.split("]")[-1]
return log_last
def run(self) -> None:
print("开始拆分")
log_last = self.bcl2fastq()
time.sleep(3)
self.finished.emit(log_last)
print("拆分完成")
if __name__ == "__main__":
task_list = []
for i in range(40):
locals()[i] = sleepTask(str(i))
task_list.append(locals()[i])
locals()[i].start()