[英]How do I run two python Flask application(project) on server in parallel
I have two different Flask project. 我有两个不同的Flask项目。 I want to run them on server on different link.
我想在不同链接的服务器上运行它们。
Currently I saw at a time one project I can see running. 目前,我一次看到一个项目可以运行。
I tried running on same port with different link, and also with different port. 我尝试在具有不同链接的同一端口上以及在不同端口上运行。 But I see it runs only one project at a time.
但是我看到它一次只运行一个项目。
Project 1 项目1
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5001,debug = True)
Project 2
I tried running 我尝试跑步
export FLASK_APP=app.py flask run --host 0.0.0.0 --port 5000
Also this way
if __name__ == '__main__': app.run(host="0.0.0.0", port="5000",debug = True)
I recently did a parallel threading operation with my own website in Flask. 我最近在Flask上对自己的网站进行了并行线程操作。 So I completely understand your confusion, though I'm going to explain this the best of my abilities.
因此,尽管我将尽我所能解释这件事,但我完全理解您的困惑。
When creating parallel operations, it's best to use multi-threading. 创建并行操作时,最好使用多线程。 Basically multi-threading is meant for splitting operations up and doing them simultaneously on the CPU.
基本上,多线程是指在CPU上拆分操作并同时进行操作。 Though this must be supported by the CPU, which most by today are supporting Multi-Threading.
尽管这必须由CPU支持,但今天大多数都支持多线程。
Anyways, with the application. 无论如何,与应用程序。 I initialized the Flask Application classes to share the data between all the threads, by using the main thread as the memory handler.
通过使用主线程作为内存处理程序,我初始化了Flask Application类以在所有线程之间共享数据。 Afterwards, I created the pages.
之后,我创建了页面。 Then within the initialization 'if statement'
(if __name__ == '__main__')
- Known as a driver class in other languages. 然后在初始化“ if语句”
(if __name__ == '__main__')
-在其他语言中称为驱动程序类。 I initialized and started the threads to do there parts of the application. 我初始化并启动线程以完成应用程序的某些部分。
Notes: Flask doesn't allow debug mode to be executed while not on the Main Thread. 注意:Flask不允许在不在主线程上时执行调试模式。 Basically meaning you cannot use the multi-threading on the Flask Apps when debugging the application, which is no problem.
基本上意味着您在调试应用程序时不能在Flask Apps上使用多线程,这没问题。 VSCode has a great output console to give me enough information to figure out the issues within the application.
VSCode具有出色的输出控制台,可为我提供足够的信息来找出应用程序中的问题。 Though... sometimes thread error finding can be.. painful at times, it's best to watch your steps when debugging.
虽然...有时发现线程错误有时会很痛苦,但最好在调试时注意步骤。
Another thing is you can still operate the threaded feature on Flask. 另一件事是,您仍然可以在Flask上操作线程功能。 Which I like to use on any Flask Application I make, because it allows better connection for the clients.
我喜欢在我制作的任何Flask应用程序上使用它,因为它可以为客户端提供更好的连接。 For example, thread is disabled;
例如,线程被禁用; the client connects and holds up the main thread, which holds it for a millisecond then releases it.
客户端连接并支撑主线程,该主线程将保留主线程一毫秒,然后释放它。 Having threaded enabled;
启用线程; allows the clients to open and release multiple requests.
允许客户端打开和释放多个请求。 Instead of all the clients piping through one thread.
而不是所有客户端通过一个线程进行管道传输。
Why would that be important? 为什么这很重要? Well, if a client runs a heavy script that has to do operations on the local host machine, then that page's request query will be taking a larger amount of time.
好吧,如果客户端运行的繁重脚本必须在本地主机上执行操作,则该页面的请求查询将花费大量时间。 In returns, makes the client hold that main thread pipe, so therefore no-one else could connect.
作为回报,使客户端拥有该主线程管道,因此没有其他人可以连接。
My Code for your Issue: 针对您的问题的我的代码:
import threading
from flask import Flask
# My typical setup for a Flask App.
# ./media is a folder that holds my JS, Imgs, CSS, etc.
app1 = Flask(__name__, static_folder='./media')
app2 = Flask(__name__, static_folder='./media')
@app1.route('/')
def index1():
return 'Hello World 1'
@app2.route('/')
def index2():
return 'Hello World 2'
# With Multi-Threading Apps, YOU CANNOT USE DEBUG!
# Though you can sub-thread.
def runFlaskApp1():
app1.run(host='127.0.0.1', port=5000, debug=False, threaded=True)
def runFlaskApp2():
app2.run(host='127.0.0.1', port=5001, debug=False, threaded=True)
if __name__ == '__main__':
# Executing the Threads seperatly.
t1 = threading.Thread(target=runFlaskApp1)
t2 = threading.Thread(target=runFlaskApp2)
t1.start()
t2.start()
PS: Run this app by doing python app.py
instead of export FLASK_APP=app.py flask run --host 0.0.0.0 --port 5000
PS:通过执行
python app.py
而不是export FLASK_APP=app.py flask run --host 0.0.0.0 --port 5000
运行此应用
Hope this helps you, and happy developing! 希望这对您有所帮助,并乐于发展!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.