fix: 资源泄漏

This commit is contained in:
helsonxiao 2024-04-04 22:52:38 +08:00
parent 205ae66a45
commit a58c3dbe6c

View File

@ -32,7 +32,6 @@ class JudgeClient(object):
self._test_case_dir = test_case_dir
self._submission_dir = submission_dir
self._pool = Pool(processes=psutil.cpu_count())
self._test_case_info = self._load_test_case_info()
self._spj_version = spj_version
@ -46,6 +45,8 @@ class JudgeClient(object):
if not os.path.exists(self._spj_exe):
raise JudgeClientError("spj exe not found")
self._pool = Pool(processes=psutil.cpu_count())
def _load_test_case_info(self):
try:
with open(os.path.join(self._test_case_dir, "info")) as f:
@ -179,10 +180,14 @@ class JudgeClient(object):
def run(self):
tmp_result = []
result = []
for test_case_file_id, _ in self._test_case_info["test_cases"].items():
tmp_result.append(self._pool.apply_async(_run, (self, test_case_file_id)))
self._pool.close()
self._pool.join()
try:
for test_case_file_id, _ in self._test_case_info["test_cases"].items():
tmp_result.append(self._pool.apply_async(_run, (self, test_case_file_id)))
except Exception as e:
raise e
finally:
self._pool.close()
self._pool.join()
for item in tmp_result:
# exception will be raised, when get() is called
# # http://stackoverflow.com/questions/22094852/how-to-catch-exceptions-in-workers-in-multiprocessing