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
答案 0 :(得分:0)
您无需在此处使用result
方法:
订户是非阻塞的,因此我们必须阻止主线程退出以允许它在后台处理消息。
def run():
future = subscriber.subscribe(full_subscription, print_and_ack_message)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
future.cancel()