'Handle requests from the client.'

def run(self):

    t0 = time.time()
    initialized = False
    timed_out = False
    while 1:
        try:
            events = self.socket.poll(TIMEOUT)
        except KeyboardInterrupt:
            time.sleep(0.1)
            continue
        if ((events == 0) and initialized):
            if timed_out:
                delta = int((time.time() - t0))
                print ('Timed    out    after    %s    sec' % delta)
                return
            timed_out = True
            continue
        timed_out = False
        initialized = True
        requests = []
        while 1:
            try:
                request = self.socket.recv_pyobj()
            except KeyboardInterrupt:
                time.sleep(0.1)
                continue
            if (request['func_name'] == 'server_quit'):
                print 'Quitting'
                sys.stdout.flush()
                return
            elif (request['func_name'] != 'server_heartbeat'):
                requests.append(request)
            else:
                print 'Got    heartbeat'
            try:
                events = self.socket.poll(0)
            except KeyboardInterrupt:
                time.sleep(0.1)
                continue
            if (events == 0):
                break
        if (not requests):
            continue
        request = requests[(-1)]
        response = dict(func_name=request['func_name'], request_id=request['request_id'])
        try:
            func = getattr(self.object, request['func_name'])
            args = request.get('args', [])
            kwargs = request.get('kwargs', {})
            response['result'] = func(*args, **kwargs)
        except Exception:
            response['error'] = traceback.format_exc()
        self.socket.send_pyobj(response)
