Producer: high-level message writer¶
-
class
gnsq.
Producer
(nsqd_tcp_addresses=[], max_backoff_duration=128, **kwargs)[source]¶ High level NSQ producer.
A Producer will connect to the nsqd tcp addresses and support async publishing (
PUB
&MPUB
&DPUB
) of messages to nsqd over the TCP protocol.Example publishing a message:
from gnsq import Producer producer = Producer('localhost:4150') producer.start() producer.publish('topic', b'hello world')
Parameters: - nsqd_tcp_addresses – a sequence of string addresses of the nsqd instances this consumer should connect to
- max_backoff_duration – the maximum time we will allow a backoff state to last in seconds. If zero, backoff wil not occur
- **kwargs –
passed to
NsqdTCPClient
initialization
-
is_running
¶ Check if the producer is currently running.
-
join
(timeout=None, raise_error=False)[source]¶ Block until all connections have closed and workers stopped.
-
multipublish
(topic, messages, block=True, timeout=None, raise_error=True)[source]¶ Publish an iterable of messages to the given topic.
Parameters: - topic – the topic to publish to
- messages – iterable of bytestrings to publish
- block – wait for a connection to become available before
publishing the message. If block is False and no connections
are available,
NSQNoConnections
is raised - timeout – if timeout is a positive number, it blocks at most
timeout
seconds before raisingNSQNoConnections
- raise_error – if
True
, it blocks until a response is received from the nsqd server, and any error response is raised. Otherwise anAsyncResult
is returned
-
on_auth
[source]¶ Emitted after a connection is successfully authenticated.
The signal sender is the consumer and the
conn
and parsedresponse
are sent as arguments.
-
on_error
[source]¶ Emitted when an error is received.
The signal sender is the consumer and the
error
is sent as an argument.
-
on_response
[source]¶ Emitted when a response is received.
The signal sender is the consumer and the ` ` is sent as an argument.
-
publish
(topic, data, defer=None, block=True, timeout=None, raise_error=True)[source]¶ Publish a message to the given topic.
Parameters: - topic – the topic to publish to
- data – bytestring data to publish
- defer – duration in milliseconds to defer before publishing (requires nsq 0.3.6)
- block – wait for a connection to become available before
publishing the message. If block is False and no connections
are available,
NSQNoConnections
is raised - timeout – if timeout is a positive number, it blocks at most
timeout
seconds before raisingNSQNoConnections
- raise_error – if
True
, it blocks until a response is received from the nsqd server, and any error response is raised. Otherwise anAsyncResult
is returned