|
1 | 1 | 'dockerjudge - A Docker Based Online Judge Engine' |
2 | 2 |
|
3 | | -from math import ceil |
| 3 | +from concurrent.futures import ThreadPoolExecutor |
4 | 4 | from pathlib import PurePosixPath |
5 | 5 |
|
6 | 6 | import docker |
|
9 | 9 | from . import processor as _processor |
10 | 10 | from .status import Status |
11 | 11 | from . import test_case |
12 | | -from .thread import Thread |
13 | 12 |
|
14 | 13 | __version__ = '1.2.5' |
15 | 14 |
|
@@ -117,24 +116,24 @@ def compile_source_code(container, processor, source, config): |
117 | 116 |
|
118 | 117 | def judge_test_cases(container, processor, tests, config): |
119 | 118 | '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) |
132 | 125 | ) |
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 |
138 | 137 |
|
139 | 138 |
|
140 | 139 | def run(container, processor, source, tests, config=None): |
|
0 commit comments