diff --git a/README.md b/README.md index 3a40763..c52c0a1 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,13 @@

# dockerjudge -[![Build Status](https://www.travis-ci.org/wangxinhe2006/dockerjudge.svg)](https://www.travis-ci.org/wangxinhe2006/dockerjudge) -[![CodeCov](https://codecov.io/gh/wangxinhe2006/dockerjudge/graph/badge.svg)](https://codecov.io/gh/wangxinhe2006/dockerjudge) +[![Build Status](https://www.travis-ci.org/piterator-org/dockerjudge.svg)](https://www.travis-ci.org/piterator-org/dockerjudge) +[![CodeCov](https://codecov.io/gh/piterator-org/dockerjudge/graph/badge.svg)](https://codecov.io/gh/piterator-org/dockerjudge) [![Python Version](https://img.shields.io/pypi/pyversions/dockerjudge.svg)](https://www.python.org/downloads/) -[![GitHub pre-release](https://img.shields.io/github/release-pre/wangxinhe2006/dockerjudge.svg)](https://github.com/wangxinhe2006/dockerjudge/releases) +[![GitHub pre-release](https://img.shields.io/github/release-pre/piterator-org/dockerjudge.svg)](https://github.com/piterator-org/dockerjudge/releases) [![PyPI](https://img.shields.io/pypi/v/dockerjudge.svg)](https://pypi.org/project/dockerjudge/#history) [![Wheel](https://img.shields.io/pypi/wheel/dockerjudge.svg)](https://pypi.org/project/dockerjudge/#files) -[![License](https://img.shields.io/github/license/wangxinhe2006/dockerjudge.svg)](LICENSE) +[![License](https://img.shields.io/github/license/piterator-org/dockerjudge.svg)](LICENSE) A Docker Based Online Judge Engine @@ -31,11 +31,11 @@ easy_install dockerjudge ``` ### From [GitHub](https://github.com/) -[wangxinhe2006/dockerjudge: A Docker Based Online Judge Engine](https://github.com/wangxinhe2006/dockerjudge) -- HTTPS: `https://github.com/wangxinhe2006/dockerjudge.git` -- SSH: `git@github.com:wangxinhe2006/dockerjudge.git` +[wangxinhe2006/dockerjudge: A Docker Based Online Judge Engine](https://github.com/piterator-org/dockerjudge) +- HTTPS: `https://github.com/piterator-org/dockerjudge.git` +- SSH: `git@github.com:piterator-org/dockerjudge.git` ```sh -git clone https://github.com/wangxinhe2006/dockerjudge.git +git clone https://github.com/piterator-org/dockerjudge.git cd dockerjudge python3 setup.py install ``` diff --git a/dockerjudge/__init__.py b/dockerjudge/__init__.py index 268202b..c08ec11 100644 --- a/dockerjudge/__init__.py +++ b/dockerjudge/__init__.py @@ -1,5 +1,6 @@ 'dockerjudge - A Docker Based Online Judge Engine' +import math from pprint import pprint import re import shlex @@ -9,7 +10,7 @@ import docker import ruamel.yaml -__version__ = '0.7.2' +__version__ = '0.8.0' class Thread(threading.Thread): @@ -79,9 +80,11 @@ def _judge(dir, container, commands, ioput, timeout, iofile) -> (str, float): def judge(settings, source='', tests=[], timeout=1, iofile=(None, None), - callback={}, client=docker.from_env()): + callback={}, split=0, client=docker.from_env()): 'Main judge function' tests = list(tests) + if not split: + split = len(tests) container = client.containers.run(settings['image'], detach=True, network_disabled=True, tty=True) try: @@ -94,21 +97,22 @@ def judge(settings, source='', tests=[], timeout=1, iofile=(None, None), if compiler.exit_code: result = [('CE', .0) for test in tests] else: - threads = [] - for i in range(len(tests)): - thread = Thread(target=_judge, - args=(i, container, - (settings.get('before_judging'), - settings['judge'], - settings.get('after_judging')), - tests[i], timeout, iofile), - callback=callback.get('judging')) - thread.start() - threads.append(thread) result = [] - for thread in threads: - thread.join() - result.append(thread.return_value) + for i in range(math.ceil(len(tests) / split)): + threads = [] + for j in range(i * split, min((i + 1) * split, len(tests))): + thread = Thread(target=_judge, + args=(j, container, + (settings.get('before_judging'), + settings['judge'], + settings.get('after_judging')), + tests[j], timeout, iofile), + callback=callback.get('judging')) + thread.start() + threads.append(thread) + for thread in threads: + thread.join() + result.append(thread.return_value) return [result, (compiler.output[1] or b'').decode()] finally: container.remove(force=True) diff --git a/tests.py b/tests.py index b89a5c6..3d51db3 100644 --- a/tests.py +++ b/tests.py @@ -1,3 +1,4 @@ +from time import time import unittest from dockerjudge import judge @@ -167,6 +168,25 @@ def judging_callback(id, status, duration): self.assertEqual(result[0][2][0], 'RE') self.assertFalse(result[1]) + def test_split(self): + t = time() + result = judge({'image': 'gcc:4.8', + 'source': 'a.cpp', + 'compile': 'g++ a.cpp', + 'judge': '%s/a.out'}, + r'#include ''\n' + r'int main() {''\n' + r' while (true)''\n' + r' ;''\n' + r'}''\n', + [('', '')] * 3, + 3, split=2) + self.assertEqual(result[0][0][0], 'TLE') + self.assertEqual(result[0][1][0], 'TLE') + self.assertEqual(result[0][2][0], 'TLE') + self.assertGreater(time() - t, 6) + self.assertFalse(result[1]) + if __name__ == '__main__': unittest.main()