SIGINT在Google pubsub_v1 / python中被忽略

时间:2019-03-16 13:07:46

标签: python google-cloud-pubsub

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()

由于某些原因,上面的代码无法在python 2.7.15的macOS,zsh,iTerm和pyenv-virtualenv上中断吗?

CTRL + C在带有此代码的终端上失败;什么也没发生,只有^C在输出中可见,并且它既不终止也不打印任何内容。怎么了?

我正在关注docs

1 个答案:

答案 0 :(得分:0)

您无需在此处使用result方法:

订户是非阻塞的,因此我们必须阻止主线程退出以允许它在后台处理消息。

def run():
    future = subscriber.subscribe(full_subscription, print_and_ack_message)
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        future.cancel()