Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- A script to show how neo4j impacts thread exits on shutdown.
- Run the script via the command line, wait for the worker thread to finish
- creating a neo db, and then hit CTRL-C.
- Compare the output of that run with a run in which ENABLE_NEO is False, and
- a different set of print statements will be run.
- '''
- import Queue
- import tempfile
- import time
- import threading
- # set the following line to false
- ENABLE_NEO = True
- class Worker(object):
- def __init__(self, queue):
- self.queue = queue
- self.db = False
- def __call__(self):
- if ENABLE_NEO:
- import neo4j
- while True:
- item = self.queue.get()
- self.queue.task_done()
- if item:
- if not self.db:
- print 'worker: creating neo db...'
- self.db = True
- if ENABLE_NEO:
- self.db = neo4j.GraphDatabase(tempfile.mkdtemp())
- print 'worker: ...done'
- print 'worker: going to sleep...'
- time.sleep(5)
- print 'worker: ...done'
- else:
- # this is a shutdown signal
- print 'worker: shutting down neo...'
- if ENBALE_NEO:
- self.db.shutdown()
- print 'worker: ...done'
- break
- def main():
- print 'starting up...'
- q = Queue.Queue()
- t = threading.Thread(group=None, target=Worker(q))
- t.start()
- print '...startup complete; press ctrl-c'
- try:
- while True:
- q.put(True)
- print 'main: going to sleep...'
- time.sleep(5)
- print 'main: ...done'
- except KeyboardInterrupt:
- print 'main: intercepted KeyboardInterrupt, enqueueing shutdown signal...'
- q.put(False)
- q.join()
- print 'main: done'
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement