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
37a39583
Commit
37a39583
authored
Apr 21, 2024
by
Yuan Zhixiang
Browse files
添加了redis缓存
parent
8db9e5a9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Dockerfile
View file @
37a39583
...
@@ -36,7 +36,11 @@ RUN tar -zxf c_backend.tar.gz && \
...
@@ -36,7 +36,11 @@ 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 psutil
RUN
pip3.12
install
flask
RUN
pip3.12
install
flask-cors
RUN
pip3.12
install
gevent
RUN
pip3.12
install
psutil
RUN
pip3.12
install
redis
# 替代旧的分析脚本(python2)
# 替代旧的分析脚本(python2)
COPY
./run_cpp_backend.py ./c_backend/
COPY
./run_cpp_backend.py ./c_backend/
...
...
backend.py
View file @
37a39583
import
redis
from
flask
import
Flask
,
request
from
flask
import
Flask
,
request
import
subprocess
import
subprocess
import
json
import
json
...
@@ -9,11 +10,13 @@ import signal
...
@@ -9,11 +10,13 @@ import signal
app
=
Flask
(
__name__
)
app
=
Flask
(
__name__
)
CORS
(
app
)
CORS
(
app
)
redis_available
=
False
# 用于测试后端是否正常运行
# 用于测试后端是否正常运行
@
app
.
route
(
'/state'
)
@
app
.
route
(
'/state'
)
def
hello_world
():
def
hello_world
():
return
'OK
'
return
'OK
.<br>redis_available: '
+
str
(
redis_available
)
@
app
.
route
(
'/visualize'
,
methods
=
[
'POST'
])
@
app
.
route
(
'/visualize'
,
methods
=
[
'POST'
])
...
@@ -26,6 +29,12 @@ def visualize():
...
@@ -26,6 +29,12 @@ def visualize():
if
code
is
None
:
if
code
is
None
:
return
{
'error'
:
'Code does not exist.'
}
return
{
'error'
:
'Code does not exist.'
}
key
=
code
+
'#'
+
stdin
if
redis_available
:
value
=
r
.
get
(
key
)
if
value
is
not
None
:
return
json
.
loads
(
str
(
value
))
# 执行分析脚本
# 执行分析脚本
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
:
...
@@ -35,6 +44,8 @@ def visualize():
...
@@ -35,6 +44,8 @@ def visualize():
elif
result
==
'timeout'
:
elif
result
==
'timeout'
:
return
{
'error'
:
'Timeout.'
}
return
{
'error'
:
'Timeout.'
}
else
:
else
:
if
redis_available
:
r
.
set
(
key
,
result
)
return
json
.
loads
(
result
)
return
json
.
loads
(
result
)
...
@@ -62,6 +73,13 @@ def run_python2_script_and_get_output(code, stdin):
...
@@ -62,6 +73,13 @@ def run_python2_script_and_get_output(code, stdin):
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
print
(
'c-backend server start.'
)
print
(
'c-backend server start.'
)
r
=
redis
.
Redis
(
host
=
'c-redis-service'
,
password
=
'c-backend'
,
port
=
6379
,
decode_responses
=
True
)
try
:
r
.
ping
()
redis_available
=
True
print
(
'redis: connected.'
)
except
redis
.
exceptions
.
ConnectionError
as
e
:
print
(
'redis: failed.'
,
e
)
# 启动后端
# 启动后端
http_server
=
WSGIServer
((
''
,
5000
),
app
)
http_server
=
WSGIServer
((
''
,
5000
),
app
)
http_server
.
serve_forever
()
http_server
.
serve_forever
()
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