0

I'm using kazoo=2.2.1, python 2.7, zookeeper 3.4.5--1. I've set up a DataWatch, and if the callback raise an exception I'd like to abort the program. I'm unable to abort with the following code

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

`

How do I abort in wrap_do_stuff()?

Lorenzo Belli
  • 1,767
  • 4
  • 25
  • 46

1 Answers1

1

DataWatch is run in a separate thread and there are specific ways to kill the main process from a thread, see Why does sys.exit() not exit when called inside a thread in Python?

Something like os._exit() does work.

Lorenzo Belli
  • 1,767
  • 4
  • 25
  • 46