Пример #1
0
    def call(self, method, message=None, timeout=None):
        """Send a message to the remote Actor this object addresses.
        Wait for a result. If a timeout in seconds is passed, raise
        eventlet.TimeoutError if no result is returned in less than the timeout.
        """
        message_id = str(uuid.uuid1())
        parsed,conn = connect(self._address)
        call_msg = {'remotecall':message_id,
                    'method':method,
                    'message':message,
                    'timeout':timeout}
        resp = conn.request('POST',parsed[2],json.dumps(call_msg,default=handle_custom))
        if timeout is None:
            cancel = None
        else:
            ## Raise any TimeoutError to the caller so they can handle it
            cancel = eventlet.Timeout(timeout, eventlet.TimeoutError)

        resp = conn.getresponse()
        stat = resp.status
        rstr = resp.read()

        if stat == 202:
            rjson = json.loads(rstr,object_hook=generate_custom)
            return rjson['message']
        elif stat == 404:
            rjson = json.loads(rstr,object_hook=generate_custom)
            raise RemoteAttributeError(rjson['invalid_method'])
        elif stat == 406:
            rjson = json.loads(rstr,object_hook=generate_custom)
            raise RemoteException(rjson['exception'])
        elif stat == 408:
            rjson = json.loads(rstr,object_hook=generate_custom)
            raise eventlet.TimeoutError(rjson['timeout'])
        else:
            raise RemoteException("Unknown remote response "+str(stat))
Пример #2
0
 def test_timeout(self):
     import time
     eventlet.Timeout(0.1, eventlet.TimeoutError())
     self.assertRaises(eventlet.TimeoutError, tpool.execute, time.sleep,
                       0.3)