本文整理汇总了Python中kombu.entity.Queue类的典型用法代码示例。如果您正苦于以下问题:Python Queue类的具体用法?Python Queue怎么用?Python Queue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Queue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_declare
def test_declare(self):
chan = get_conn().channel()
b = Queue('foo', self.exchange, 'foo', channel=chan)
self.assertTrue(b.is_bound)
b.declare()
self.assertIn('exchange_declare', chan)
self.assertIn('queue_declare', chan)
self.assertIn('queue_bind', chan)
示例2: test_eq
def test_eq(self):
q1 = Queue("xxx", Exchange("xxx", "direct"), "xxx")
q2 = Queue("xxx", Exchange("xxx", "direct"), "xxx")
self.assertEqual(q1, q2)
self.assertFalse(q1.__eq__(True))
q3 = Queue("yyy", Exchange("xxx", "direct"), "xxx")
self.assertNotEqual(q1, q3)
示例3: test_declare
def test_declare(self):
chan = Channel()
b = Queue("foo", self.exchange, "foo", channel=chan)
self.assertTrue(b.is_bound)
b.declare()
self.assertIn("exchange_declare", chan)
self.assertIn("queue_declare", chan)
self.assertIn("queue_bind", chan)
示例4: MessConsumer
class MessConsumer(object):
def __init__(self, mess, connection, name, exchange_name):
self._mess = mess
self._conn = connection
self._name = name
self._exchange = Exchange(name=exchange_name, type='topic',
durable=False, auto_delete=True) #TODO parameterize
self._channel = None
self._ops = {}
self.connect()
def connect(self):
self._channel = self._conn.channel()
self._queue = Queue(channel=self._channel, name=self._name,
exchange=self._exchange, routing_key=self._name)
self._queue.declare()
self._consumer = Consumer(self._channel, [self._queue],
callbacks=[self._callback])
self._consumer.consume()
def consume(self):
while True:
self._conn.drain_events()
def _callback(self, body, message):
reply_to = message.headers.get('reply-to')
#TODO error handling for message format
op = body['op']
args = body['args']
kwargs = body['kwargs']
#TODO error handling for unknown op
op_fun = self._ops[op]
ret, err = None, None
try:
ret = op_fun(*args, **kwargs)
except Exception:
err = sys.exc_info()
finally:
if reply_to:
if err:
tb = traceback.format_exception(*err)
err = (err[0].__name__, str(err[1]), tb)
reply = dict(result=ret, error=err)
self._mess.reply(reply_to, reply)
message.ack()
def add_op(self, name, fun):
self._ops[name] = fun
示例5: test_also_binds_exchange
def test_also_binds_exchange(self):
chan = Channel()
b = Queue("foo", self.exchange)
self.assertFalse(b.is_bound)
self.assertFalse(b.exchange.is_bound)
b = b.bind(chan)
self.assertTrue(b.is_bound)
self.assertTrue(b.exchange.is_bound)
self.assertIs(b.channel, b.exchange.channel)
self.assertIsNot(b.exchange, self.exchange)
示例6: _connect
def _connect(self, channel):
self._queue = Queue(channel=channel, **self._queue_kwargs)
self._queue.declare()
self._consumer = Consumer(channel, [self._queue],
callbacks=[self._callback])
self._consumer.consume()
示例7: connect
def connect(self):
self._channel = self._conn.channel()
self._queue = Queue(channel=self._channel, name=self._name,
exchange=self._exchange, routing_key=self._name)
self._queue.declare()
self._consumer = Consumer(self._channel, [self._queue],
callbacks=[self._callback])
self._consumer.consume()
示例8: call
def call(self, name, op, *args, **kwargs):
# create a direct exchange and queue for the reply
# TODO probably better to pool these or something?
msg_id = uuid.uuid4().hex
exchange = Exchange(name=msg_id, type='direct',
durable=False, auto_delete=True) #TODO parameterize
# check out a connection from the pool
with connections[self._conn].acquire(block=True) as conn:
channel = conn.channel()
queue = Queue(channel=channel, name=msg_id, exchange=exchange,
routing_key=msg_id, exclusive=True, durable=False,
auto_delete=True)
queue.declare()
# I think this can be done without gevent directly, but need to
# learn more about kombu first
messages = []
def _callback(body, message):
messages.append(body)
message.ack()
consumer = Consumer(channel=channel, queues=[queue],
callbacks=[_callback])
d = dict(op=op, args=args, kwargs=kwargs)
headers = {'reply-to' : msg_id}
with producers[self._conn].acquire(block=True) as producer:
producer.publish(d, routing_key=name, headers=headers)
with consumer:
# only expecting one event
conn.drain_events()
msg_body = messages[0]
if msg_body.get('error'):
raise Exception(*msg_body['error'])
else:
return msg_body.get('result')
示例9: connect
def connect(self):
self._channel = self._conn.channel()
self._queue = Queue(channel=self._channel, name=self._name,
exchange=self._exchange, routing_key=self._name,
durable=self._dashi.durable,
auto_delete=self._dashi.auto_delete)
self._queue.declare()
self._consumer = Consumer(self._channel, [self._queue],
callbacks=[self._callback])
self._consumer.consume()
示例10: test_declare_but_no_exchange
def test_declare_but_no_exchange(self):
q = Queue('a')
q.queue_declare = Mock()
q.queue_bind = Mock()
q.exchange = None
q.declare()
q.queue_declare.assert_called_with(False, passive=False)
q.queue_bind.assert_called_with(False)
示例11: connect
def connect(self):
self._channel = self._conn.channel()
if self._sysname is not None:
name = "%s.%s" % (self._sysname, self._name)
else:
name = self._name
self._queue = Queue(channel=self._channel, name=name,
exchange=self._exchange, routing_key=name,
durable=self._dashi.durable,
auto_delete=self._dashi.auto_delete)
self._queue.declare()
self._consumer = Consumer(self._channel, [self._queue],
callbacks=[self._callback])
self._consumer.consume()
示例12: DashiConsumer
class DashiConsumer(object):
def __init__(self, dashi, connection, name, exchange, sysname=None):
self._dashi = dashi
self._conn = connection
self._name = name
self._exchange = exchange
self._sysname = sysname
self._channel = None
self._ops = {}
self._cancelled = False
self._consumer_lock = threading.Lock()
self._last_heartbeat_check = datetime.min
self.connect()
def connect(self):
self._channel = self._conn.channel()
if self._sysname is not None:
name = "%s.%s" % (self._sysname, self._name)
else:
name = self._name
self._queue = Queue(channel=self._channel, name=name,
exchange=self._exchange, routing_key=name,
durable=self._dashi.durable,
auto_delete=self._dashi.auto_delete)
self._queue.declare()
self._consumer = Consumer(self._channel, [self._queue],
callbacks=[self._callback])
self._consumer.consume()
def disconnect(self):
self._consumer.cancel()
self._channel.close()
self._conn.release()
def consume(self, count=None, timeout=None):
# hold a lock for the duration of the consuming. this prevents
# multiple consumers and allows cancel to detect when consuming
# has ended.
if not self._consumer_lock.acquire(False):
raise Exception("only one consumer thread may run concurrently")
try:
if count:
i = 0
while i < count and not self._cancelled:
self._consume_one(timeout)
i += 1
else:
while not self._cancelled:
self._consume_one(timeout)
finally:
self._consumer_lock.release()
self._cancelled = False
def _consume_one(self, timeout=None):
# do consuming in a busy-ish loop, checking for cancel. There doesn't
# seem to be an easy way to interrupt drain_events other than the
# timeout. This could probably be added to kombu if needed. In
# practice cancellation is likely infrequent (except in tests) so this
# should hold for now. Can use a long timeout for production and a
# short one for tests.
inner_timeout = self._dashi.consumer_timeout
elapsed = 0
# keep trying until a single event is drained or timeout hit
while not self._cancelled:
self.heartbeat()
try:
self._conn.drain_events(timeout=inner_timeout)
break
except socket.timeout:
if timeout:
elapsed += inner_timeout
if elapsed >= timeout:
raise
if elapsed + inner_timeout > timeout:
inner_timeout = timeout - elapsed
def heartbeat(self):
if self._dashi._heartbeat_interval is None:
return
time_between_tics = timedelta(seconds=self._dashi._heartbeat_interval / 2)
if self._dashi.consumer_timeout > time_between_tics.seconds:
msg = "dashi consumer timeout (%s) must be half or smaller than the heartbeat interval %s" % (
self._dashi.consumer_timeout, self._dashi._heartbeat_interval)
#.........这里部分代码省略.........
示例13: test_as_dict
def test_as_dict(self):
q = Queue('foo', self.exchange, 'rk')
d = q.as_dict(recurse=True)
self.assertEqual(d['exchange']['name'], self.exchange.name)
示例14: test_unbind
def test_unbind(self):
b = Queue('foo', self.exchange, 'foo', channel=get_conn().channel())
b.unbind()
self.assertIn('queue_unbind', b.channel)
示例15: test_delete
def test_delete(self):
b = Queue('foo', self.exchange, 'foo', channel=get_conn().channel())
b.delete()
self.assertIn('queue_delete', b.channel)