简体   繁体   English

如何在前台运行 flask 自定义 cli 命令?

[英]How to run flask custom cli command in foreground?

I'd like to start consuming messages from RabbitMQ server.我想开始使用来自 RabbitMQ 服务器的消息。 I wrote flask custom CLI command in which I connect to my RabbitMQ channel and start listing for messages.我编写了 flask 自定义 CLI 命令,我在其中连接到我的 RabbitMQ 频道并开始列出消息。 Default behavior of flask CLI command it to exit after triggered by CLI function is executed. flask CLI 命令的默认行为在执行 CLI function 触发后退出。 My question is, how can I start command in Flask to run it in foreground?我的问题是,如何在 Flask 中启动命令以在前台运行它? So when running flask my_custom_commad It will stay up?那么运行flask my_custom_commad的时候会熬夜吗?

It was simpler than I thought.这比我想象的要简单。 I used threading package, with this solution my CPU utilization is 0% (contrary to infinite loop)我使用threading package,使用此解决方案我的 CPU 利用率为 0%(与无限循环相反)

from threading import Event

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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