【问题标题】:kazoo DataWatch does not exit the program when exception is raised引发异常时,kazoo DataWatch 不会退出程序
【发布时间】:2018-05-04 23:49:16
【问题描述】:

我使用的是 kazoo=2.2.1、python 2.7、zookeeper 3.4.5--1。 我已经设置了一个 DataWatch,如果回调引发异常,我想中止程序。我无法使用以下代码中止

from kazoo.recipe.watchers import DataWatch
from utils.zookeeper import Zookeeper
import time
import sys


def do_stuff(data, stat):
   raise NotImplementedError("abort")


def wrap_do_stuff(data, stat):
    try:
        do_stuff(data, stat)
    except Exception as e:
        if data != '':
            print e.message

            # This does not exit the program
            # sys.exit(-1)

            # This does not exit the program
            # raise e

            # This does not exit the program, just stop the watcher
            return False


def start():
    zookeeper_client = Zookeeper.init_from_config().client
    # Someone is writing into this node after creation
    new_node_path = zookeeper_client.create('/test/',
                                        ephemeral=True, sequence=True)
    path_to_watch = new_node_path
    print path_to_watch
    datawatcher = DataWatch(zookeeper_client, path_to_watch, wrap_do_stuff)

    while True:
        time.sleep(2)
        print "still alive"


if __name__ == '__main__':
    start()

`

如何中止wrap_do_stuff()

【问题讨论】:

    标签: python apache-zookeeper kazoo


    【解决方案1】:

    DataWatch 在单独的线程中运行,有特定的方法可以从线程中杀死主进程,请参阅Why does sys.exit() not exit when called inside a thread in Python?

    os._exit() 之类的东西确实有效。

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 1970-01-01
      • 2017-03-02
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 2016-06-26
      相关资源
      最近更新 更多