Advertisement
Guest User

neo4j shutdown script

a guest
Nov 2nd, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. '''
  2. A script to show how neo4j impacts thread exits on shutdown.
  3.  
  4. Run the script via the command line, wait for the worker thread to finish
  5. creating a neo db, and then hit CTRL-C.
  6.  
  7. Compare the output of that run with a run in which ENABLE_NEO is False, and
  8. a different set of print statements will be run.
  9.  
  10. '''
  11.  
  12. import Queue
  13. import tempfile
  14. import time
  15. import threading
  16.  
  17. # set the following line to false
  18. ENABLE_NEO = True
  19.  
  20. class Worker(object):
  21.     def __init__(self, queue):
  22.         self.queue = queue
  23.         self.db = False
  24.        
  25.     def __call__(self):
  26.         if ENABLE_NEO:
  27.             import neo4j
  28.        
  29.         while True:
  30.             item = self.queue.get()
  31.             self.queue.task_done()
  32.             if item:
  33.                 if not self.db:
  34.                     print 'worker: creating neo db...'
  35.                     self.db = True
  36.                     if ENABLE_NEO:
  37.                         self.db = neo4j.GraphDatabase(tempfile.mkdtemp())
  38.                     print 'worker: ...done'
  39.                 print 'worker: going to sleep...'
  40.                 time.sleep(5)
  41.                 print 'worker: ...done'
  42.             else:
  43.                 # this is a shutdown signal
  44.                 print 'worker: shutting down neo...'
  45.                 if ENBALE_NEO:
  46.                     self.db.shutdown()
  47.                 print 'worker: ...done'
  48.                 break
  49.  
  50. def main():
  51.     print 'starting up...'
  52.     q = Queue.Queue()
  53.     t = threading.Thread(group=None, target=Worker(q))
  54.     t.start()
  55.     print '...startup complete; press ctrl-c'
  56.    
  57.     try:
  58.         while True:
  59.             q.put(True)
  60.             print 'main: going to sleep...'
  61.             time.sleep(5)
  62.             print 'main: ...done'
  63.     except KeyboardInterrupt:
  64.         print 'main: intercepted KeyboardInterrupt, enqueueing shutdown signal...'
  65.         q.put(False)
  66.         q.join()
  67.        
  68.     print 'main: done'
  69.  
  70. if __name__ == '__main__':
  71.     main()
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement