简体   繁体   中英

How to start a flask app from within a python script in a separate thread?

I have a script that is extracting data from an internal service and storing the count of data types in memory like this:

metrics = {
   "new_accounts": 152,
   "acquisitions": 2005,
   ...
}

That is being done in memory and that whole process is kicked off by the subscribe method in this script:

if __name__ == "__main__":
    loop = asyncio.get_event_loop()

    for signal in [signal.SIGHUP, signal.SIGTERM, signal.SIGINT]:
        loop.add_signal_handler(
            signal, lambda s=signal: asyncio.create_task(close_subscriptions(s, loop)))

    for subscription in SUBSCRIPTION_TYPES:
        loop.create_task(subscribe(subscription))
    loop.run_forever()

I want to expose that metrics data through an endpoint in flask for monitoring purposes. In a thread, can I start the flask app in this script like this:

if __name__ == "__main__":
   ...
    for subscription in SUBSCRIPTION_TYPES:
        loop.create_task(subscribe(subscription))
    ... <start flask app in a separate thread>
    loop.run_forever()

Does this make sense? I already have the flask app running locally but it doesn't have access to this metrics data. Is this the way I need to go about this?

Try to put Your metric to Queue:

https://docs.python.org/2/library/queue.html

Flask endpoint can read all data from Queue, and expose last value as endpoint data

Cheers, Fenrir

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM