python - SIGINT 在谷歌 pubsub_v1/python 中被忽略
问题描述
from google.cloud import pubsub_v1
def run():
# created full_* vars here...
future = subscriber.subscribe(full_subscription, print_and_ack_message)
try:
future.result()
except KeyboardInterrupt: # this doesn't work for some reason...
logging.info("Subscription terminated...")
future.cancel()
except BaseException as exc:
logging.info("Other %s", type(exc))
if __name__ == '__main__':
run()
上面的代码不能在 macOS、zsh、iTerm 和 pyenv-virtualenv 上用 python 2.7.15 中断,出于某种原因?
CTRL+C 使用此代码从终端失败;什么都没有发生,只有^C
在输出中可见,它既不终止也不打印任何东西。怎么了?
我正在关注文档
解决方案
您不需要在result
此处使用该方法:
订阅者是非阻塞的,所以我们必须阻止主线程退出以允许它在后台处理消息。
def run():
future = subscriber.subscribe(full_subscription, print_and_ack_message)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
future.cancel()
推荐阅读
- marklogic - 文档查询 - MarkLogic 10 中的 dls:documents-query() 与 MarkLogic 7 有何不同?
- python - 附加到镶木地板文件的 EMR Spark 步骤正在覆盖镶木地板文件
- angular - 表单数组内的输入被窃听
- php - Symfony 4.3 + ApiPlatform + Accept-Language
- oracle - 我可以将 INSERT ALL 与多个记录类型变量一起使用吗?
- amazon-web-services - 使用 AWS Lambda 定期监控远程资源的状态
- jmeter - PerfMon 使用非 gui 模式在 JMeter 中报告
- reactjs - 排序数组后渲染未排序
- javascript - Lua,Scrapy / Splash:单击没有href的按钮
- linux - OutOfMemoryException by TaskScheduler in highly concurrent async asp.net core application