0

I have two different Flask project. 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

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)
2
  • Why are they two separate projects? Commented Jan 9, 2019 at 17:40
  • One is Text Classification and Another is Image classification. Both usage different data, library and concept. So keeping it separate does not make sense? @DanielRoseman Thanks for reply Commented Jan 9, 2019 at 17:43

1 Answer 1

7

I recently did a parallel threading operation with my own website in 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. Though this must be supported by the CPU, which most by today are supporting Multi-Threading.

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. Afterwards, I created the pages. Then within the initialization 'if statement'(if __name__ == '__main__') - Known as a driver class in other languages. 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. Basically meaning you cannot use the multi-threading on the Flask Apps when debugging the application, which is no problem. VSCode has a great output console to give me enough information to figure out the issues within the application. 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. Which I like to use on any Flask Application I make, because it allows better connection for the clients. 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

Hope this helps you, and happy developing!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for detailed answer. Actually my app1 and app2 are independent program. Think like I have two different projects to demo, and host them on my server. Do you think above code will help me for that?
@nlpker I'm not quite understanding, are you trying to run two applications in two different processes and share similar data between it? Because, when it comes to the multi-threading method that I provided above, could be one easier and faster way of doing such. But much more harder to manage in the long run. Though, if you choice to separate it by process and not by thread. Then you're best off to do SQL or Json requests between the client. Such as: app1 is the main user website, and app2 being the api of your site - this would use Json requests..
This is awesome! thanks guys! I think this will be really useful! this is why I love stack overflow! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.