【OJ】测评机实现-django/python

tasks.py文件(celery)

通过views.py中的submit函数中的evaluate_submission.delay(sub.id)方法,对一个子进程程序进行:编译、产生.out文件、对比testcase文件、返回测评结果。
sub内包含html中通过表单(form)的post请求提交的内容:问题id,提交者,语言,代码。

补充编译指令解释

g++:这是调用 g++ 编译器的命令。
{codefile}:这是你要编译的 C++ 源代码文件名。你需要将 {codefile} 替换为你实际的源代码文件名。
-O2:这是一个优化标志,告诉编译器对代码进行二级优化。这可以使代码运行更快,但也可能增加编译时间。
-Wall:这个标志启用编译过程中的所有警告信息,可以帮助识别代码中的潜在问题。
-lm:这个标志告诉连接器链接数学库(libm),提供了一些可能在程序中使用的与数学有关的函数。
--static:这个选项告诉连接器创建一个静态链接的可执行文件,这意味着所有必需的库将被包含在可执行文件中。这会使可执行文件变得更大,但可以避免共享库的依赖问题。
-DONLINE_JUDGE:这个选项定义了一个名为 ONLINE_JUDGE 的预处理宏,可以根据它是否为在线评测系统进行有条件地编译代码。
-o main:这个选项指定编译器生成的输出文件的名称。在这种情况下,输出文件将命名为 main

copy
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
from celery import shared_task import os # 产生子进程的模块 import subprocess # 对应的models,声明的数据库表 from machine.models.coder.coder import Coder from machine.models.problem.problem import Problem from machine.models.problem.testcase import TestCase from machine.models.problem.submission import Submission # 声明judge文件下的函数 from machine.judge import compiles # 装饰器 通过name属性找到该函数并调用 @shared_task(name = "evaluate_submission") def evaluate_submission(sub_id): try: # 读取到提交元素 submission = Submission.objects.get(pk=sub_id) except: return # 提交者 username = submission.submitter.user.username # 当前题目,题目提交数+1 cur_problem = submission.problem cur_problem.num_submissions += 1 # 时间限制 tl = cur_problem.time_limit timeout_lst = ["timeout", str(tl)] # 获取语言 langu = submission.lang # 定义codefile(代码文件名) codefile = { "C" :"{}_{}.c", "CPP" :"{}_{}.cpp", "JAVA" :"{}_{}.java", "PYTH3":"{}_{}.py", } filename = '' compiler = '' filemir = "{}_{}".format(username,sub_id) filename = codefile[langu].format(username,sub_id) # 保存代码到文件 user_code = open("submissions/{}".format(filename),"w+") print(submission.code,file = user_code) user_code.close() # 定义编译命令 build_cmd = { "C" :"gcc {} -o main -Wall -lm -O2 -std=c99 --static -DONLINE_JUDGE", "CPP" :"g++ {} -O2 -Wall -lm --static -DONLINE_JUDGE -o", "JAVA" :"javac {}", "PYTH3":"python3 -m py_compile {}", } # 开启子进程编译代码->可执行文件 p = subprocess.Popen(build_cmd[langu].format(filename),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) stdout, stderr = p.communicate() # 获取当前问题所有的测试用例 testcases = TestCase.objects.filter(problem=cur_problem) # 执行测试用例 cnt = 1 for tc in testcases: # 测试用力的.in文件和.out文件 input_filename = tc.input_file.name output_filename = tc.output_file.name # 定义执行文件和用户运行后输出文件 executable = "{}_{}_{}.out".format(username,sub_id,cnt) user_output_filename = "testcases/ans_{}_{}_{}.out".format(username,sub_id,cnt) cnt = cnt + 1 # optional_lst是check_output执行必须的变量, # 通过run.py执行测试用例并保存到user_output_filename optional_lst = ["python3","run.py","process/{}".format(executable),input_filename,user_output_filename,str(tl),langu] run_code = subprocess.check_output(optional_lst,shell = False) # run_code解码 run_code = int(run_code.decode("utf-8")) # 开始判断 111 return 'eva_now'

run.py函数

copy
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
import sys import subprocess if __name__ == "__main__": try: executable = sys.argv[1] input_filename = sys.argv[2] output_filename = sys.argv[3] tl = sys.argv[4] lang = sys.argv[5] except IndexError: sys.exit(-1) RUN_COMMAND = { "PYTH3": "python {executable}", "C": "./{executable}", "CPP": "./{executable}", } input_file = open(input_filename, "r") output_file = open(output_filename, "w") command = RUN_COMMAND[lang].format(executable) returncode = subprocess.call(["timeout", tl,command], stdin=input_file, stdout=output_file) print(returncode) input_file.close() output_file.close()

本文作者:iuk11

本文链接:https://www.cnblogs.com/iuk11/p/17337393.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   iuk11  阅读(263)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起