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 && \ ...@@ -36,7 +36,7 @@ RUN tar -zxf c_backend.tar.gz && \
rm Python-3.12.2.tgz rm Python-3.12.2.tgz
# 安装依赖 # 安装依赖
RUN pip3.12 install flask flask-cors gevent RUN pip3.12 install flask flask-cors gevent psutil
# 替代旧的分析脚本(python2) # 替代旧的分析脚本(python2)
COPY ./run_cpp_backend.py ./c_backend/ COPY ./run_cpp_backend.py ./c_backend/
......
...@@ -3,6 +3,8 @@ import subprocess ...@@ -3,6 +3,8 @@ import subprocess
import json import json
from gevent.pywsgi import WSGIServer from gevent.pywsgi import WSGIServer
from flask_cors import CORS from flask_cors import CORS
import psutil
import signal
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app)
...@@ -28,9 +30,10 @@ def visualize(): ...@@ -28,9 +30,10 @@ def visualize():
result = run_python2_script_and_get_output(code, stdin) result = run_python2_script_and_get_output(code, stdin)
if result is None: if result is None:
return {'error': 'Analysis failed.'} return {'error': 'Analysis failed.'}
else: elif len(result) == 0:
if len(result) == 0:
return {'error': 'Analysis failed.'} return {'error': 'Analysis failed.'}
elif result == 'timeout':
return {'error': 'Timeout.'}
else: else:
return json.loads(result) return json.loads(result)
...@@ -42,12 +45,19 @@ def run_python2_script_and_get_output(code, stdin): ...@@ -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) process = subprocess.Popen([python2_interpreter, script_path, code, 'c', stdin], stdout=subprocess.PIPE)
output, error = process.communicate() try:
output, error = process.communicate(None, 30)
if process.returncode != 0: if process.returncode != 0:
# raise Exception('Python 2 script failed with error: {}'.format(error.decode('utf-8'))) # raise Exception('Python 2 script failed with error: {}'.format(error.decode('utf-8')))
return None return None
return output.decode('utf-8') 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__': 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