本文整理汇总了Python中kombu.messaging.Producer.send方法的典型用法代码示例。如果您正苦于以下问题:Python Producer.send方法的具体用法?Python Producer.send怎么用?Python Producer.send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kombu.messaging.Producer
的用法示例。
在下文中一共展示了Producer.send方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: EventDispatcher
# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import send [as 别名]
class EventDispatcher(object):
"""Send events as messages.
:param connection: Connection to the broker.
:keyword hostname: Hostname to identify ourselves as,
by default uses the hostname returned by :func:`socket.gethostname`.
:keyword enabled: Set to :const:`False` to not actually publish any events,
making :meth:`send` a noop operation.
:keyword channel: Can be used instead of `connection` to specify
an exact channel to use when sending events.
:keyword buffer_while_offline: If enabled events will be buffered
while the connection is down. :meth:`flush` must be called
as soon as the connection is re-established.
You need to :meth:`close` this after use.
"""
def __init__(self, connection=None, hostname=None, enabled=True,
channel=None, buffer_while_offline=True, app=None):
self.app = app_or_default(app)
self.connection = connection
self.channel = channel
self.hostname = hostname or socket.gethostname()
self.enabled = enabled
self.buffer_while_offline = buffer_while_offline
self._lock = threading.Lock()
self.publisher = None
self._outbound_buffer = deque()
if self.enabled:
self.enable()
def enable(self):
conf = self.app.conf
self.enabled = True
channel = self.channel or self.connection.channel()
self.publisher = Producer(channel,
exchange=event_exchange,
serializer=conf.CELERY_EVENT_SERIALIZER)
def disable(self):
self.enabled = False
if self.publisher is not None:
if not self.channel: # close auto channel.
self.publisher.channel.close()
self.publisher = None
def send(self, type, **fields):
"""Send event.
:param type: Kind of event.
:keyword \*\*fields: Event arguments.
"""
if not self.enabled:
return
self._lock.acquire()
event = Event(type, hostname=self.hostname, **fields)
try:
try:
self.publisher.publish(event,
routing_key=type.replace("-", "."))
except Exception, exc:
if not self.buffer_while_offline:
raise
self._outbound_buffer.append((event, exc))
finally:
self._lock.release()
def flush(self):
while self._outbound_buffer:
event, _ = self._outbound_buffer.popleft()
self.publisher.send(event)
def close(self):
"""Close the event dispatcher."""
self._lock.locked() and self._lock.release()
self.publisher and self.publisher.channel.close()