Advertisement
Guest User

Untitled

a guest
Feb 7th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. import eventlet
  2. eventlet.monkey_patch()
  3.  
  4. import json
  5. import time
  6. import zookeeper
  7.  
  8. from eventlet import tpool
  9.  
  10. TIMES = []
  11.  
  12. class Session(object):
  13.     """Session interface that makes use of the python-zookeeper bindings.
  14.    """
  15.     def __init__(self):
  16.         self.connected = False
  17.         self.handle = None
  18.  
  19.     def handle_session_event(self, handle, event_type, conn_state, path):
  20.         """Fires when there is a change in the state of the zk session.
  21.  
  22.        :param handle: ZK session handle.
  23.        :param event_type: The type of zk event.
  24.        :param conn_state: The new state of the connection.
  25.        :param path: The path that resulted in this callback (if any).
  26.        """
  27.         if conn_state == zookeeper.CONNECTED_STATE:
  28.             TIMES.append(time.time() - self.time_started)
  29.             self.connected = True
  30.             return
  31.         if conn_state == zookeeper.CONNECTING_STATE:
  32.             self.connected = False
  33.             return
  34.  
  35.         raise NotImplementedError('Unhandled Event')
  36.  
  37.     def connect(self):
  38.         session_id = -1
  39.         zk_pass = ''
  40.         recv_timeout = 10000
  41.         zk_endpoint = 'localhost:2181'
  42.  
  43.         self.time_started = time.time()
  44.         self.handle = tpool.execute(zookeeper.init,
  45.                                     zk_endpoint,
  46.                                     self.handle_session_event,
  47.                                     recv_timeout,
  48.                                     (session_id, zk_pass))
  49.  
  50.  
  51.         for x in range(15):
  52.             if self.connected:
  53.                 return
  54.             eventlet.sleep(0.01)
  55.         else:
  56.             print "Session did not establish within 150ms"
  57.             while 1:
  58.                 if self.connected:
  59.                     return
  60.                 eventlet.sleep(0.01)
  61.             return
  62.  
  63.     def disconnect(self):
  64.         """Close the active zookeeper connection.
  65.        """
  66.         if self.handle is not None:
  67.             tpool.execute(zookeeper.close, self.handle)
  68.             self.connected = False
  69.             self.handle = None
  70.  
  71. if __name__ == '__main__':
  72.     zklog_fd = open('/tmp/zk_log.log', 'a+')
  73.     zookeeper.set_log_stream(zklog_fd)
  74.  
  75.     count = 0
  76.     for x in range(3000):
  77.         session = Session()
  78.         session.connect()
  79.         session.disconnect()
  80.         count += 1
  81.         if count % 100 == 0:
  82.             print count
  83.  
  84.     with open('output', 'w') as f:
  85.         json.dump(TIMES, f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement