我有一个Flask应用程序,app.py就像:
@app.route("/", methods=['GET','POST'])
def index():
......
return render_template("index.html", ....<args for jinja> ...)
def runAPP(thread_name, port):
try:
app.run(debug = True, port=port, use_reloader=False)
except ex:
print("exit with error: %r", ex)
此外,我还有一些功能可以让我的网络服务器与另一台服务器连接。
def keepAlive(thread_name):
while True:
sendHeatbeatToServer(dns, port)
time.sleep(30)
return
if __name__ == '__main__':
# thread 1
t1=threading.Thread( target=runAPP, args=("Thread-1", ..., ) )
t2=threading.Thread( target=keepAlive, args=("Thread-2", ) )
t1.start()
t2.start()
我希望在Docker容器中安装此应用程序,但在尝试使用两个分支时会出现问题。
- 1. use "flask run app.py --host 0.0.0.0" in DockerFile, then it only execute the render part and ignore multithreads
- 2. use CMD["python", "app.py"] then it somehow not working with browser on Windows (The container is linux, and my outside docker env is Win 10, web browser is IE / Chrome on Win10), and I get error: ERR_EMPTY_RESPONSE
有没有办法让这个简单的应用程序在docker中运行,同时KeepAlive应用程序也在这个docker中运行?
更多细节:POST请求需要使用与KeepAlive相同的服务客户端和TCP连接,我不能将它们放在单独的脚本中。