models.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:OJ 作者: nishantcoder97 项目源码 文件源码
def run(self, inp=None):
        name = self.get_obj_file_name()
        sandbox = Sandbox()
        cmd = self.get_run_command(name, sandbox)
        start = timer()
        stdout = b''
        stderr = b''
        env = os.environ.copy()
        r = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             stdout=subprocess.PIPE, bufsize=4*1024,
                             cwd=MEDIA_ROOT, preexec_fn=os.setsid,env=env)
        try:
            if inp is not None:
                stdout, stderr = r.communicate(timeout=timeout, input=inp.encode())
            else:
                stdout, stderr = r.communicate(timeout=timeout)
            print('STDOUT : ' + str(stdout, "utf-8"))
            print('STDERR : ' + str(stderr, "utf-8"))
        except subprocess.TimeoutExpired as e:
            print("Timeout expired")
            os.killpg(r.pid, signal.SIGINT)
            r.returncode = 124
        print('Return Code : ' + str(r.returncode))
        if self.lang != 'python':
            os.remove(MEDIA_ROOT+'/'+name)
        print('Elapsed seconds: {:.2f}'.format(timer() - start))
        sandbox.delete_sandbox()
        return Result(timer() - start, r.returncode, stdout)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号