From 2ba1fa56691214a9f239da1d34ee11fbcd6225d0 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Wed, 12 Aug 2015 14:56:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- judge/controller/tasks.py | 10 ++++++---- judge/judger/run.py | 14 +++++++------- problem/views.py | 2 +- submission/views.py | 7 +++++++ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/judge/controller/tasks.py b/judge/controller/tasks.py index d87cde96..42b88ee0 100644 --- a/judge/controller/tasks.py +++ b/judge/controller/tasks.py @@ -3,9 +3,11 @@ from __future__ import absolute_import from judge.controller.celery import app import subprocess32 as subprocess -subprocess.call("ping baidu.com", timeout=5) - @app.task -def judge(source_code, language, test_case_id): - pass \ No newline at end of file +def judge(solution_id, time_limit, memory_limit, test_case_id): + subprocess.call("/usr/bin/docker run -t -i --privileged -v /var/test_case/:/var/judger/test_case/ " + "-v /var/code/:/var/judger/code/ judger python judge/judger/run.py " + "--solution_id %s --time_limit %s --memory_limit %s --test_case_id %s" % + (solution_id, str(time_limit), str(memory_limit), test_case_id), + timeout=(time_limit / 100) * 20) diff --git a/judge/judger/run.py b/judge/judger/run.py index f75e81d8..40da1dcb 100644 --- a/judge/judger/run.py +++ b/judge/judger/run.py @@ -13,12 +13,12 @@ from settings import judger_workspace from oj import settings # 简单的解析命令行参数 -# 参数有 -solution_id -max_cpu_time -max_memory -test_case_id -# 获取到的值是['xxx.py', '-solution_id', '1111', '-max_cpu_time', '1000', '-max_memory', '100', '-test_case_id', 'aaaa'] +# 参数有 -solution_id -time_limit -memory_limit -test_case_id +# 获取到的值是['xxx.py', '-solution_id', '1111', '-time_limit', '1000', '-memory_limit', '100', '-test_case_id', 'aaaa'] args = sys.argv solution_id = args[2] -max_cpu_time = args[4] -max_memory = args[6] +time_limit = args[4] +memory_limit = args[6] test_case_id = args[8] @@ -48,9 +48,9 @@ except Exception as e: client = JudgeClient(language_code=language, exe_path=exe_path, - max_cpu_time=1000000, - max_real_time=200000, - max_memory=1000, + max_cpu_time=int(time_limit), + max_real_time=int(time_limit) * 2, + max_memory=int(memory_limit), test_case_dir="/var/judger/test_case/" + str(test_case_id) + "/") print client.run() diff --git a/problem/views.py b/problem/views.py index e8f3e9a7..7669816d 100644 --- a/problem/views.py +++ b/problem/views.py @@ -187,7 +187,7 @@ class TestCaseUploadAPIView(APIView): return error_response(u"测试用例文件不完整,缺少" + name[0] + ".in") problem_test_dir = rand_str() - test_case_dir = settings.TEST_CASE_DIR + "test_case/" + problem_test_dir + "/" + test_case_dir = settings.TEST_CASE_DIR + problem_test_dir + "/" # 得到了合法的测试用例文件列表 然后去解压缩 os.mkdir(test_case_dir) diff --git a/submission/views.py b/submission/views.py index 8af26f1f..54256809 100644 --- a/submission/views.py +++ b/submission/views.py @@ -9,7 +9,9 @@ from rest_framework.views import APIView from django.conf import settings from judge.judger.result import result +from judge.controller.tasks import judge from account.decorators import login_required +from problem.models import Problem from utils.shortcuts import serializer_invalid_response, error_response, success_response from .serializers import CreateSubmissionSerializer @@ -32,10 +34,15 @@ class SubmissionnAPIView(APIView): data = serializer.data data["user_id"] = request.user.id data["result"] = result["waiting"] + try: + problem = Problem.objects.get(id=data["problem_id"]) + except Problem.DoesNotExist: + return error_response(u"题目不存在") mongodb_setting = settings.DATABASES["mongodb"] connection = pymongo.MongoClient(host=mongodb_setting["HOST"], port=mongodb_setting["PORT"]) collection = connection["oj"]["oj_submission"] submission_id = str(collection.insert_one(data).inserted_id) + judge.deply(submission_id, problem.max_cpu_time, problem_) return success_response({"submission_id": submission_id}) else: return serializer_invalid_response(serializer)