当前位置: 首页>>代码示例>>Python>>正文


Python Producer.send方法代码示例

本文整理汇总了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()
开发者ID:66laps,项目名称:celery,代码行数:86,代码来源:__init__.py


注:本文中的kombu.messaging.Producer.send方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。