简体   繁体   English

如何在服务器上并行运行两个python Flask应用程序(项目)

[英]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.

相关问题 如何在https服务器上运行flask python应用程序? - How to run a flask python application on https server? How do I tell Jenkins to run a Python Flask application, then Selenium tests, then close the Flask application? - How do I tell Jenkins to run a Python Flask application, then Selenium tests, then close the Flask application? 如何在Flask python应用程序中运行Elasticsearch的实例? - How do I run an instance of Elasticsearch from within a Flask python application? 如何诊断 Python Flask 应用程序中仅在应用程序与 uWSGI 一起运行时出现的问题? - How do I diagnose problems in a Python Flask application that only occur when app is run with uWSGI? 如何在启用 ssl 的情况下运行 flask_socketio 应用程序? - How do i run a flask_socketio application with ssl enabled? 如何在 linux 中与不同的参数并行运行 python 脚本? - How do I run a python script in parallel with different arguments in linux? 如何使用asyncio在python3中运行并行作业? - How do I run parallel jobs in python3 with asyncio? 如何在Python中并行运行os.walk? - How do I run os.walk in parallel in Python? 如何与Perl并行运行大量Python程序? - How do I run numerous Python programs with Perl in parallel? 如何自动化 cmd 命令来运行 flask 服务器 - How do I automate cmd commands to run flask server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM