4

Python flask threaded works for me, but I want multiprocess in my app, because I need 2 processes running and one has time-consuming task. Here is the demo:

there is two API in flask: '/' for index and '/longtime' for time consuming task, demo for short 'time.sleep(20)'

def worker():
name = multiprocessing.current_process().pid
print name, 'Starting'
main()
print name, 'Exiting'

from web.fapp import app
def my_flask():
    name = multiprocessing.current_process().pid
    print name, 'Starting'
    app.run(threaded=True)
    print name, 'Exiting'


import multiprocessing
if __name__ == '__main__':
#     main()
# multithread
    worker_1 = multiprocessing.Process(name='gevent_task', target=worker)
    flask_service = multiprocessing.Process(name='flask_test_app', target=my_flask)
    flask_service.start()
    worker_1.start()

in this way, flask will not work in threaded mode. How can I find other solutions?

wiwengweng
  • 352
  • 1
  • 4
  • 16

1 Answers1

0

You can use Flask and Tornado together.

If you need background tasks or scheduling you can use Flask + Celery + (Rabbitmq or Redis) as broker

Community
  • 1
  • 1
Ivan Kvas
  • 446
  • 6
  • 11