Esempio 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 something 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 i in range(nbthreads):
         t = spawn(self._serve_clients)
         t.setName('Worker%i' % i)
         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 = spawn(self._poll_inactive_clients)
     self.polling_thread.setName('PollingThread')
Esempio n. 2
0
def connect_thread(service=VoidService,
                   config={},
                   remote_service=VoidService,
                   remote_config={}):
    """starts an rpyc server on a new thread, bound to an arbitrary port,
    and connects to it over a socket.

    :param service: the local service to expose (defaults to Void)
    :param config: configuration dict
    :param server_service: the remote service to expose (of the server; defaults to Void)
    :param server_config: remote configuration dict (of the server)
    """
    listener = socket.socket()
    listener.bind(("localhost", 0))
    listener.listen(1)

    def server(listener=listener):
        with closing(listener):
            client = listener.accept()[0]
        conn = connect_stream(SocketStream(client),
                              service=remote_service,
                              config=remote_config)
        try:
            conn.serve_all()
        except KeyboardInterrupt:
            interrupt_main()

    spawn(server)
    host, port = listener.getsockname()
    return connect(host, port, service=service, config=config)
Esempio n. 3
0
 def _listen(self):
     if self.active:
         return
     super(ThreadPoolServer, self)._listen()
     # setup the thread pool for handling requests
     self.workers = []
     for i in range(self.nbthreads):
         t = spawn(self._serve_clients)
         t.setName('Worker%i' % i)
         self.workers.append(t)
     # setup a thread for polling inactive connections
     self.polling_thread = spawn(self._poll_inactive_clients)
     self.polling_thread.setName('PollingThread')
Esempio n. 4
0
    def serve_threaded(self, thread_count=10):  # serving
        """Serves all requests and replies for as long as the connection is alive.

        CAVEAT: using non-immutable types that require a netref to be constructed to serve a request,
        or invoking anything else that performs a sync_request, may timeout due to the sync_request reply being
        received by another thread serving the connection. A more conventional approach where each client thread
        opens a new connection would allow `ThreadedServer` to naturally avoid such multiplexing issues and
        is the preferred approach for threading procedures that invoke sync_request. See issue #345
        """
        def _thread_target():
            try:
                while True:
                    self.serve(None)
            except (socket.error, select_error, IOError):
                if not self.closed:
                    raise
            except EOFError:
                pass

        try:
            threads = [spawn(_thread_target) for _ in range(thread_count)]

            for thread in threads:
                thread.join()
        finally:
            self.close()
Esempio n. 5
0
    def serve_threaded(self, thread_count=10):
        """Serves all requests and replies for as long as the connection is
        alive."""
        def _thread_target():
            try:
                while True:
                    self.serve(None)
            except (socket.error, select_error, IOError):
                if not self.closed:
                    raise
            except EOFError:
                pass

        try:
            threads = [spawn(_thread_target) for _ in range(thread_count)]

            for thread in threads:
                thread.join()
        finally:
            self.close()
Esempio n. 6
0
 def __init__(self, conn, callback=None):
     self._conn = conn
     self._active = True
     self._callback = callback
     self._thread = spawn(self._bg_server)
Esempio n. 7
0
 def _accept_method(self, sock):
     spawn(self._authenticate_and_serve_client, sock)
Esempio n. 8
0
 def _register(self):
     if self.auto_register:
         self.auto_register = False
         spawn(self._bg_register)