1 Star 0 Fork 0

jecosine/MOJ

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
monitor.py 2.69 KB
一键复制 编辑 原始数据 按行查看 历史
silver 提交于 2020-01-29 07:55 . TEST
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):
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jecosine/MOJ.git
[email protected]:jecosine/MOJ.git
jecosine
MOJ
MOJ
master

搜索帮助