Search code examples
pythonflaskpython-multiprocessing

Flask API to get data from another process with Queues


I have a code that make some analysis for me and save informations on an multiprocessing.Queue.

E.g.

import multiprocessing 
import time 

q = multiprocessing.Queue

for i in range(1000):
    q.put(i)
    time.sleep(1)

I want start an flask REST API that returns the value on q, but I'm not finding any way to do this.

I tried this, but doesn't nothing, just close.

from multiprocessing import Process
import flask

class API():
    def __init__(self):
        
        self.p = Process(name='FlaskTest', target=self.start , args=())
    
    def start(self, host='0.0.0.0', port='5000', debug=True):

        app = flask.Flask(__name__)

        @app.route('/', methods=['GET'])
        def home():
            return "<h1>Se isso der certo vou ficar muito feliz</h1>"

        app.run(host=host, port=port, debug=debug)

if __name__ == '__main__':
    api = API()

Solution

  • You're not calling the start() method on your API object so it's only creating the API() object and exiting.