Skip to content

Commit 86c63af

Browse files
authored
Merge pull request #350 from Kvieta1990/master
Limix max number of runs for MTS routine
2 parents bc15636 + ad8c313 commit 86c63af

File tree

4 files changed

+106
-8
lines changed

4 files changed

+106
-8
lines changed

addie/main.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from addie.utilities.job_monitor_thread import JobMonitorThread
2828
from addie.utilities.job_status_handler import JobStatusHandler
29+
from addie.utilities.job_status_handler import JobStatusHandlerMTS
2930
from addie.utilities.logbook_thread import LogbookThread
3031
from addie.utilities.logbook_handler import LogbookHandler
3132

@@ -490,13 +491,31 @@ def launch_job_manager(
490491
thread_index=thread_index)
491492
job_handler.start()
492493

494+
# job utility for mantidtotalscattering
495+
def launch_job_manager_mts(
496+
self,
497+
job_name='',
498+
all_commands=None,
499+
thread_index=-1):
500+
job_handler = JobStatusHandlerMTS(parent=self, job_name=job_name,
501+
all_commands=all_commands,
502+
thread_index=thread_index)
503+
job_handler.start()
504+
493505
def kill_job(self, row=-1):
494506
job_row = self.job_list[row]
495507
parent = psutil.Process(job_row['pid'])
496508
for child in parent.children(recursive=True):
497-
child.kill()
498-
# child.wait()
499-
parent.kill()
509+
if child.status != psutil.STATUS_ZOMBIE:
510+
for count, item in enumerate(self.job_list):
511+
job_row_tmp = item
512+
if job_row_tmp['pid'] == child.pid:
513+
job_row_tmp['status'] = "killed"
514+
job_row_tmp['pid'] = None
515+
self.job_list[count] = job_row_tmp
516+
child.kill()
517+
if parent.name() != 'addie':
518+
parent.kill()
500519

501520
table_widget = self.job_monitor_interface.ui.tableWidget
502521
table_widget.removeCellWidget(row, 2)

addie/processing/mantid/launch_reduction.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def run_mantid(parent):
2525
full_reduction_filename = os.path.join(
2626
os.path.expanduser('~'), '.mantid', 'addie.json')
2727
print('writing out full table to "{}"'.format(full_reduction_filename))
28+
all_commands = list()
29+
all_files = list()
2830
for row in range(num_rows):
2931
dictionary, activate = exporter.retrieve_row_info(row)
3032
if activate is True:
@@ -119,12 +121,36 @@ def run_mantid(parent):
119121
"Container": container_type}
120122

121123
if final_validator(dict_out_tmp):
122-
filename_to_run = os.path.join(os.path.expanduser('~'), '.mantid', 'JSON_output', 'running_tmp.json')
124+
filename_to_run = os.path.join(os.path.expanduser('~'),
125+
'.mantid',
126+
'JSON_output',
127+
'running_tmp_' + str(row) + '.json')
123128
with open(filename_to_run, 'w') as outfile:
124129
json.dump(dict_out_tmp, outfile, indent=2)
125130
_script_to_run = "bash /SNS/NOM/shared/scripts/mantidtotalscattering/run_mts.sh " + filename_to_run
126-
parent.launch_job_manager(job_name='MantidTotalScattering',
127-
script_to_run=_script_to_run)
131+
all_commands.append(_script_to_run.split())
132+
all_files.append(filename_to_run)
133+
134+
limit = 4
135+
136+
if len(all_commands) > limit:
137+
all_commands = list()
138+
num_runs = len(all_files)
139+
chunk_size = num_runs // limit
140+
left_over = num_runs - (limit - 1) * chunk_size
141+
for i in range(limit):
142+
command_tmp = ""
143+
if i < limit - 1:
144+
for j in range(i * chunk_size, (i + 1) * chunk_size):
145+
command_tmp += (" " + all_files[j])
146+
else:
147+
for j in range(left_over):
148+
command_tmp += (" " + all_files[i * chunk_size + j])
149+
command_tmp = "bash /SNS/NOM/shared/scripts/mantidtotalscattering/run_mts_all.sh" + command_tmp
150+
all_commands.append(command_tmp.split())
151+
152+
parent.launch_job_manager_mts(job_name='MantidTotalScattering',
153+
all_commands=all_commands)
128154

129155

130156
def log_error(type_err, key):
@@ -373,3 +399,5 @@ def final_validator(final_dict):
373399
else:
374400
log_error(1, '["Merging"]["QBinning"]')
375401
return False
402+
403+
return True

addie/utilities/job_monitor_thread.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def _checking_status_of_jobs(self):
3434
_item = QTableWidgetItem("Done!")
3535
self.job_monitor_interafce.ui.tableWidget.setItem(_row, 2, _item)
3636
else:
37-
if not process.status() == 'sleeping':
37+
if _job['status'] == 'killed':
3838
self.job_monitor_interafce.ui.tableWidget.removeCellWidget(_row, 2)
39-
_item = QTableWidgetItem("Done!")
39+
_item = QTableWidgetItem("Killed!")
4040
self.job_monitor_interafce.ui.tableWidget.setItem(_row, 2, _item)

addie/utilities/job_status_handler.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,54 @@ def get_launch_time(self):
5454

5555
def start(self):
5656
pass
57+
58+
59+
class JobStatusHandlerMTS(object):
60+
61+
def __init__(self, parent=None, job_name='', all_commands=None, thread_index=-1):
62+
self.parent = parent
63+
64+
if self.parent.job_monitor_interface is None:
65+
job_ui = JobMonitorInterface(parent=self.parent)
66+
job_ui.show()
67+
QApplication.processEvents()
68+
self.parent.job_monitor_interface = job_ui
69+
job_ui.launch_logbook_thread()
70+
else:
71+
self.parent.job_monitor_interface.activateWindow()
72+
job_ui = self.parent.job_monitor_interface
73+
74+
if job_name == '':
75+
return
76+
77+
job_list = self.parent.job_list
78+
79+
for cmd in all_commands:
80+
p = subprocess.Popen(cmd)
81+
82+
new_job = {'job_name': job_name,
83+
'time': self.get_launch_time(),
84+
'status': 'processing',
85+
'pid': p.pid,
86+
'subprocess': p}
87+
job_list.append(new_job)
88+
self.parent.job_list = job_list
89+
90+
job_ui.refresh_table(job_list)
91+
92+
def update_logbook_text(self, text):
93+
print(text)
94+
95+
def get_local_time(self):
96+
local_hour_offset = time.timezone / 3600.
97+
_gmt_time = time.gmtime()
98+
[year, month, day, hour, minute, seconds, _wday, _yday, _isds] = _gmt_time
99+
return [year, month, day, hour-local_hour_offset, minute, seconds]
100+
101+
def get_launch_time(self):
102+
local_time = self.get_local_time()
103+
return "%d %d %d %d:%d:%d" % (local_time[0], local_time[1], local_time[2],
104+
local_time[3], local_time[4], local_time[5])
105+
106+
def start(self):
107+
pass

0 commit comments

Comments
 (0)