Exemplo n.º 1
0
 def __init__(self, *args, **kwargs):
     '''Initializes a ThreadPoolServer. In particular, instantiate the thread pool.'''
     # get the number of threads in the pool
     nbthreads = 20
     if 'nbThreads' in kwargs:
         nbthreads = kwargs['nbThreads']
         del kwargs['nbThreads']
     # get the request batch size
     self.request_batch_size = 10
     if 'requestBatchSize' in kwargs:
         self.request_batch_size = kwargs['requestBatchSize']
         del kwargs['requestBatchSize']
     # init the parent
     Server.__init__(self, *args, **kwargs)
     # a queue of connections having somethign to process
     self._active_connection_queue = Queue.Queue()
     # declare the pool as already active
     self.active = True
     # setup the thread pool for handling requests
     self.workers = []
     for _ in range(nbthreads):
         t = threading.Thread(target = self._serve_clients)
         t.setName('ThreadPoolWorker')
         t.daemon = True
         t.start()
         self.workers.append(t)
     # a polling object to be used be the polling thread
     self.poll_object = poll()
     # a dictionary fd -> connection
     self.fd_to_conn = {}
     # setup a thread for polling inactive connections
     self.polling_thread = threading.Thread(target = self._poll_inactive_clients)
     self.polling_thread.setName('PollingThread')
     self.polling_thread.setDaemon(True)
     self.polling_thread.start()
Exemplo n.º 2
0
 def __init__(self, *args, **kwargs):
     """Initializes a ThreadPoolServer. In particular, instantiate the thread pool."""
     # get the number of threads in the pool
     nbthreads = 20
     if 'nbThreads' in kwargs:
         nbthreads = kwargs['nbThreads']
         del kwargs['nbThreads']
     # get the request batch size
     self.request_batch_size = 10
     if 'requestBatchSize' in kwargs:
         self.request_batch_size = kwargs['requestBatchSize']
         del kwargs['requestBatchSize']
     # init the parent
     Server.__init__(self, *args, **kwargs)
     # a queue of connections having somethign to process
     self._active_connection_queue = Queue.Queue()
     # declare the pool as already active
     self.active = True
     # setup the thread pool for handling requests
     self.workers = []
     for _ in range(nbthreads):
         t = threading.Thread(target = self._serve_clients)
         t.setName('ThreadPoolWorker')
         t.daemon = True
         t.start()
         self.workers.append(t)
     # a polling object to be used be the polling thread
     self.poll_object = poll()
     # a dictionary fd -> connection
     self.fd_to_conn = {}
     # setup a thread for polling inactive connections
     self.polling_thread = threading.Thread(target = self._poll_inactive_clients)
     self.polling_thread.setName('PollingThread')
     self.polling_thread.setDaemon(True)
     self.polling_thread.start()
Exemplo n.º 3
0
 def poll(self, timeout):
     """indicates whether the stream has data to read (within *timeout*
     seconds)"""
     timeout = Timeout(timeout)
     try:
         p = poll(
         )  # from lib.compat, it may be a select object on non-Unix platforms
         p.register(self.fileno(), "r")
         while True:
             try:
                 rl = p.poll(timeout.timeleft())
             except select_error:
                 ex = sys.exc_info()[1]
                 if ex.args[0] == errno.EINTR:
                     continue
                 else:
                     raise
             else:
                 break
     except ValueError:
         # if the underlying call is a select(), then the following errors may happen:
         # - "ValueError: filedescriptor cannot be a negative integer (-1)"
         # - "ValueError: filedescriptor out of range in select()"
         # let's translate them to select.error
         ex = sys.exc_info()[1]
         raise select_error(str(ex))
     return bool(rl)
Exemplo n.º 4
0
 def poll(self, timeout):
     """indicates whether the stream has data to read (within *timeout*
     seconds)"""
     timeout = Timeout(timeout)
     try:
         p = poll()   # from lib.compat, it may be a select object on non-Unix platforms
         p.register(self.fileno(), "r")
         while True:
             try:
                 rl = p.poll(timeout.timeleft())
             except select_error:
                 ex = sys.exc_info()[1]
                 if ex.args[0] == errno.EINTR:
                     continue
                 else:
                     raise
             else:
                 break
     except ValueError:
         # if the underlying call is a select(), then the following errors may happen:
         # - "ValueError: filedescriptor cannot be a negative integer (-1)"
         # - "ValueError: filedescriptor out of range in select()"
         # let's translate them to select.error
         ex = sys.exc_info()[1]
         raise select_error(str(ex))
     return bool(rl)
Exemplo n.º 5
0
 def __init__(self, *args, **kwargs):
     '''Initializes a ThreadPoolServer. In particular, instantiate the thread pool.'''
     # get the number of threads in the pool
     self.nbthreads = kwargs.pop('nbThreads', 20)
     self.request_batch_size = kwargs.pop('requestBatchSize', 10)
     # init the parent
     Server.__init__(self, *args, **kwargs)
     # a queue of connections having something to process
     self._active_connection_queue = Queue.Queue()
     # a dictionary fd -> connection
     self.fd_to_conn = {}
     # a polling object to be used be the polling thread
     self.poll_object = poll()
Exemplo n.º 6
0
 def __init__(self, *args, **kwargs):
     '''Initializes a ThreadPoolServer. In particular, instantiate the thread pool.'''
     # get the number of threads in the pool
     self.nbthreads = kwargs.pop('nbThreads', 20)
     self.request_batch_size = kwargs.pop('requestBatchSize', 10)
     # init the parent
     Server.__init__(self, *args, **kwargs)
     # a queue of connections having something to process
     self._active_connection_queue = Queue.Queue()
     # a dictionary fd -> connection
     self.fd_to_conn = {}
     # a polling object to be used be the polling thread
     self.poll_object = poll()