text
stringlengths 0
828
|
|---|
# use correlation_id to deal with concurrent RPC calls. See:
|
# http://www.rabbitmq.com/tutorials/tutorial-six-python.html
|
msg_id = uuid.uuid4().hex
|
# expire the reply queue shortly after the timeout. it will be
|
# (lazily) deleted by the broker if we don't clean it up first
|
queue_arguments = {'x-expires': int((timeout + 1) * 1000)}
|
queue = Queue(name=msg_id, exchange=self._exchange, routing_key=msg_id,
|
durable=False, queue_arguments=queue_arguments)
|
messages = []
|
event = threading.Event()
|
def _callback(body, message):
|
messages.append(body)
|
message.ack()
|
event.set()
|
d = dict(op=operation, args=args)
|
headers = {'reply-to': msg_id, 'sender': self.add_sysname(self.name)}
|
dest = self.add_sysname(name)
|
def _declare_and_send(channel):
|
consumer = Consumer(channel, (queue,), callbacks=(_callback,))
|
with Producer(channel) as producer:
|
producer.publish(d, routing_key=dest, headers=headers,
|
exchange=self._exchange, serializer=self._serializer)
|
return consumer
|
log.debug(""sending call to %s:%s"", dest, operation)
|
with connections[self._pool_conn].acquire(block=True) as conn:
|
consumer, channel = self.ensure(conn, _declare_and_send)
|
try:
|
self._consume(conn, consumer, timeout=timeout, until_event=event)
|
# try to delete queue, but don't worry if it fails (will expire)
|
try:
|
queue = queue.bind(channel)
|
queue.delete(nowait=True)
|
except Exception:
|
log.exception(""error deleting queue"")
|
finally:
|
conn.maybe_close_channel(channel)
|
msg_body = messages[0]
|
if msg_body.get('error'):
|
raise_error(msg_body['error'])
|
else:
|
return msg_body.get('result')"
|
562,"def handle(self, operation, operation_name=None, sender_kwarg=None):
|
""""""Handle an operation using the specified function
|
@param operation: function to call for this operation
|
@param operation_name: operation name. if unspecified operation.__name__ is used
|
@param sender_kwarg: optional keyword arg on operation to feed in sender name
|
""""""
|
if not self._consumer:
|
self._consumer = DashiConsumer(self, self._conn,
|
self._name, self._exchange, sysname=self._sysname)
|
self._consumer.add_op(operation_name or operation.__name__, operation,
|
sender_kwarg=sender_kwarg)"
|
563,"def cancel(self, block=True):
|
""""""Cancel a call to consume() happening in another thread
|
This could take up to DashiConnection.consumer_timeout to complete.
|
@param block: if True, waits until the consumer has returned
|
""""""
|
if self._consumer:
|
self._consumer.cancel(block=block)"
|
564,"def link_exceptions(self, custom_exception=None, dashi_exception=None):
|
""""""Link a custom exception thrown on the receiver to a dashi exception
|
""""""
|
if custom_exception is None:
|
raise ValueError(""custom_exception must be set"")
|
if dashi_exception is None:
|
raise ValueError(""dashi_exception must be set"")
|
self._linked_exceptions[custom_exception] = dashi_exception"
|
565,"def ensure(self, connection, func, *args, **kwargs):
|
""""""Perform an operation until success
|
Repeats in the face of connection errors, pursuant to retry policy.
|
""""""
|
channel = None
|
while 1:
|
try:
|
if channel is None:
|
channel = connection.channel()
|
return func(channel, *args, **kwargs), channel
|
except (connection.connection_errors, IOError):
|
self._call_errback()
|
channel = self.connect(connection)"
|
566,"def re_tab(s):
|
""""""Return a tabbed string from an expanded one.""""""
|
l = []
|
p = 0
|
for i in range(8, len(s), 8):
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.