Commit 3ac4c368 authored by Yuan Zhixiang's avatar Yuan Zhixiang
Browse files

添加任务时间限制30s

parent 11758e4a
......@@ -36,7 +36,7 @@ RUN tar -zxf c_backend.tar.gz && \
rm Python-3.12.2.tgz
# 安装依赖
RUN pip3.12 install flask flask-cors gevent
RUN pip3.12 install flask flask-cors gevent psutil
# 替代旧的分析脚本(python2)
COPY ./run_cpp_backend.py ./c_backend/
......
......@@ -3,6 +3,8 @@ import subprocess
import json
from gevent.pywsgi import WSGIServer
from flask_cors import CORS
import psutil
import signal
app = Flask(__name__)
CORS(app)
......@@ -28,11 +30,12 @@ def visualize():
result = run_python2_script_and_get_output(code, stdin)
if result is None:
return {'error': 'Analysis failed.'}
elif len(result) == 0:
return {'error': 'Analysis failed.'}
elif result == 'timeout':
return {'error': 'Timeout.'}
else:
if len(result) == 0:
return {'error': 'Analysis failed.'}
else:
return json.loads(result)
return json.loads(result)
# 调用核心分析脚本
......@@ -42,12 +45,19 @@ def run_python2_script_and_get_output(code, stdin):
process = subprocess.Popen([python2_interpreter, script_path, code, 'c', stdin], stdout=subprocess.PIPE)
output, error = process.communicate()
if process.returncode != 0:
# raise Exception('Python 2 script failed with error: {}'.format(error.decode('utf-8')))
return None
return output.decode('utf-8')
try:
output, error = process.communicate(None, 30)
if process.returncode != 0:
# raise Exception('Python 2 script failed with error: {}'.format(error.decode('utf-8')))
return None
return output.decode('utf-8')
except subprocess.TimeoutExpired:
parent_pid = process.pid
children = psutil.Process(parent_pid).children(recursive=True)
for child in children:
child.send_signal(signal.SIGKILL)
process.send_signal(signal.SIGKILL)
return 'timeout'
if __name__ == '__main__':
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment