Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Yuan Zhixiang
c-backend-docker
Commits
3ac4c368
Commit
3ac4c368
authored
Apr 19, 2024
by
Yuan Zhixiang
Browse files
添加任务时间限制30s
parent
11758e4a
Changes
2
Show whitespace changes
Inline
Side-by-side
Dockerfile
View file @
3ac4c368
...
...
@@ -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/
...
...
backend.py
View file @
3ac4c368
...
...
@@ -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,9 +30,10 @@ def visualize():
result
=
run_python2_script_and_get_output
(
code
,
stdin
)
if
result
is
None
:
return
{
'error'
:
'Analysis failed.'
}
else
:
if
len
(
result
)
==
0
:
elif
len
(
result
)
==
0
:
return
{
'error'
:
'Analysis failed.'
}
elif
result
==
'timeout'
:
return
{
'error'
:
'Timeout.'
}
else
:
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
()
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__'
:
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment