简体   繁体   中英

How to run flask custom cli command in foreground?

I'd like to start consuming messages from RabbitMQ server. I wrote flask custom CLI command in which I connect to my RabbitMQ channel and start listing for messages. Default behavior of flask CLI command it to exit after triggered by CLI function is executed. My question is, how can I start command in Flask to run it in foreground? So when running flask my_custom_commad It will stay up?

It was simpler than I thought. I used threading package, with this solution my CPU utilization is 0% (contrary to infinite loop)

from threading import Event

# My RabbitMQ code
# code etc...
# code etc...
# code etc...

# End of CLI function
Event().wait()

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