代码拉取完成,页面将自动刷新
import subprocess
import os
import psutil
import time
# normal_file = open("file.in", "r")
# incomp_file = open("filerr.in", "r")
# invalid_file = open("fileinv.in","r")
# null_file = open("filenull.in","r")
# fs = [normal_file, incomp_file, invalid_file, null_file]
# # seek all file
# def seekfiles():
# global fs
# for f in fs:
# f.seek(0)
# filenames = ["file.in","filerr.in","fileinv.in","filenull.in"]
# def test(fname):
# input_process = subprocess.Popen(["cat", fname], stdout=subprocess.PIPE)
# exe_process = subprocess.Popen(["a.exe"], stdin=input_process.stdout, stdout=subprocess.PIPE)
# print("pid:", exe_process.pid)
# pc = psutil.Process(exe_process.pid)
class Toj:
def __init__(self, input_text, code):
self.code = code
self.input_text = input_text
self.output_text = ""
self.rss = 0
self.delta = 0.0
self.compile_log = ""
# def cndb(self):
def receive_data(self):
# clean old file
if os.path.exists("00.in"):
os.system("rm -f 00.in")
if os.path.exists("main.cpp"):
os.system("rm -f main.cpp")
# write to local file
with open("00.in","wb") as f:
f.write(self.input_text.encode("utf-8"))
with open("main.cpp", "wb") as f:
f.write(self.code.encode("utf-8"))
def test(self):
self.receive_data()
#compile test
# check whether a.exe exist
if os.path.exists("a.exe"):
os.system("rm -f a.exe")
te = subprocess.Popen(["pwd"], stdout = subprocess.PIPE)
print(te.communicate())
self.compile_process = subprocess.Popen(["g++", "main.cpp", "-o", os.path.join(os.path.abspath("."),"a.exe")], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.compile_log = self.compile_process.communicate()
_ = self.compile_process.wait()
if(self.compile_process.poll()):
print("Compile Error:\n", self.compile_log[1].decode("utf-8"))
else:
print("Compile Success")
t1 = time.time()
input_process = subprocess.Popen(["cat", "00.in"], stdout=subprocess.PIPE)
self.exe_process = subprocess.Popen(["a.exe"], stdin=input_process.stdout, stdout = subprocess.PIPE)
p = psutil.Process(self.exe_process.pid)
while(time.time() - t1 < 10):
if self.exe_process.poll() != None:
break
# self.delta = self.parseTime(time.time() - t1)
self.delta = time.time() - t1
if self.delta > 10:
print("Your program runs more than 10s!")
self.exe_process.terminate()
print(p.memory_full_info())
# def send_data(self):
# def parseMem(self):
# def parseTime(self, t):
# if t > 1000
# def pack_data(self):
# def watcher(self):
# def generae_log(self):
# # functions
# def restart(self):
# def stop(self):
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。