async def main(): try_init_cgroup() sandbox = await create_sandbox() gcc = Compiler('/usr/bin/gcc', ['gcc', '-std=c99', '-o', '/out/foo', '/in/foo.c'], 'foo.c', 'foo', ['foo']) await gcc.prepare( sandbox, b"""#include <stdio.h> int main(void) { int a, b; scanf("%d%d", &a, &b); printf("%d\\n", a + b); }""") package, _ = await gcc.build(sandbox) for case in read_legacy_cases( path.join(path.dirname(__file__), 'testdata/1000.zip')): logger.info(await case.judge(sandbox, package)) for i in range(10): logger.info(await APlusBCase(randint(0, 32767), randint(0, 32767)).judge(sandbox, package))
async def daemon(): # 尝试初始化cgroups,其名称源自控制组群(control groups)的简写,是Linux内核的一个功能,用来限制、控制与分离一个进程组的资源(如CPU、内存、磁盘输入输出等)。 try_init_cgroup() # 创建异步回话连接jv4服务端 # config['server_url'] 获取配置文件数据 async with VJ4Session(config['server_url']) as session: while True: try: # 登陆JV4服务器 await session.login_if_needed(config['uname'], config['password']) # 运行两个协程 done, pending = await wait([do_judge(session), do_noop(session)], return_when=FIRST_COMPLETED) for task in pending: task.cancel() await gather(*done) except Exception as e: logger.exception(e) # 打印等待log logger.info('Retrying after %d seconds', RETRY_DELAY_SEC) # 等待30秒 await sleep(RETRY_DELAY_SEC)
from asyncio import gather, get_event_loop, sleep from io import BytesIO from jd4.api import VJ4Session from jd4.case import read_legacy_cases from jd4.cache import cache_open, cache_invalidate from jd4.cgroup import try_init_cgroup from jd4.config import config, save_config from jd4.langs import langs from jd4.log import logger from jd4.sandbox import create_sandbox from jd4.status import STATUS_COMPILE_ERROR, STATUS_SYSTEM_ERROR, STATUS_JUDGING, STATUS_COMPILING RETRY_DELAY_SEC = 30 try_init_cgroup() loop = get_event_loop() sandbox = loop.run_until_complete(create_sandbox()) class CompileError(Exception): pass class SystemError(Exception): pass class JudgeHandler: def __init__(self, session, request, ws): self.session = session
def setUpClass(cls): try_init_cgroup() cls.cases = list(read_cases(open(path.join( path.dirname(__file__), 'testdata/aplusb.zip'), 'rb')))
def setUpClass(cls): try_init_cgroup() cls.case = APlusBCase(1, 2, 200000000, 33554432, 10)
def setUpClass(cls): try_init_cgroup() cls.cases = list( read_legacy_cases( path.join(path.dirname(__file__), 'testdata/1000.zip')))