Skip to content
This repository was archived by the owner on Jul 22, 2022. It is now read-only.

Commit 786c59f

Browse files
committed
concurrent.futures.ThreadPoolExecutor
1 parent ad0e4e2 commit 786c59f

File tree

3 files changed

+26
-49
lines changed

3 files changed

+26
-49
lines changed

dockerjudge/__init__.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'dockerjudge - A Docker Based Online Judge Engine'
22

3-
from math import ceil
3+
from concurrent.futures import ThreadPoolExecutor
44
from pathlib import PurePosixPath
55

66
import docker
@@ -9,7 +9,6 @@
99
from . import processor as _processor
1010
from .status import Status
1111
from . import test_case
12-
from .thread import Thread
1312

1413
__version__ = '1.2.5'
1514

@@ -117,24 +116,24 @@ def compile_source_code(container, processor, source, config):
117116

118117
def judge_test_cases(container, processor, tests, config):
119118
'Judge test cases'
120-
res = []
121-
for i in range(ceil(len(tests) / config.setdefault('threads',
122-
len(tests)))):
123-
threads = []
124-
for j in range(i * config['threads'],
125-
min((i + 1) * config['threads'], len(tests))):
126-
threads.append(
127-
Thread(
128-
target=test_case.__init__,
129-
args=(container, processor, j + 1, tests[j], config),
130-
callback=config['callback'].get('judge')
131-
)
119+
with ThreadPoolExecutor(max_workers=config.get('threads')) as executor:
120+
futures = []
121+
for i, test in zip(range(1, len(tests) + 1), tests):
122+
futures.append(
123+
executor.submit(test_case.__init__,
124+
container, processor, i, test, config)
132125
)
133-
threads[-1].start()
134-
for thread in threads:
135-
thread.join()
136-
res.append(thread.return_value)
137-
return res
126+
futures[-1].add_done_callback(done_callback)
127+
return [future.result()[2] for future in futures]
128+
129+
130+
def done_callback(future):
131+
'Callback'
132+
result = future.result()
133+
try:
134+
result[0]['callback'].get('judge')(result[1], *result[2])
135+
except TypeError:
136+
pass
138137

139138

140139
def run(container, processor, source, tests, config=None):

dockerjudge/test_case.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212

1313
def __init__(container, processor, i, ioput, config):
1414
'Copy binary files to `i` and judge'
15-
container.exec_run(f'cp -r 0 {i}', workdir=str(processor.workdir))
16-
exec_run(container, processor.before_judge, f'{processor.workdir}/{i}')
17-
res = judge(container, processor, i, ioput, config)
18-
exec_run(container, processor.after_judge, f'{processor.workdir}/{i}')
19-
return res
15+
try:
16+
container.exec_run(f'cp -r 0 {i}', workdir=str(processor.workdir))
17+
exec_run(container, processor.before_judge, f'{processor.workdir}/{i}')
18+
res = judge(container, processor, i, ioput, config)
19+
exec_run(container, processor.after_judge, f'{processor.workdir}/{i}')
20+
return config, i - 1, res
21+
except Exception: # pylint: disable = broad-except
22+
return config, i - 1, [Status.UE, (None, None), .0]
2023

2124

2225
def _get_io_file_path(ioro, processor, i, config):

dockerjudge/thread.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)