Message – an NSQ message

class nsq.Message(id, body, timestamp, attempts)

A class representing a message received from nsqd.

If you want to perform asynchronous message processing use the nsq.Message.enable_async() method, pass the message around, and respond using the appropriate instance method.

Generates the following events that can be listened to with nsq.Message.on():

  • finish
  • requeue
  • touch

NOTE: A calling a message’s nsq.Message.finish() and nsq.Message.requeue() methods positively and negatively impact the backoff state, respectively. However, sending the backoff=False keyword argument to nsq.Message.requeue() is considered neutral and will not impact backoff state.

Parameters:
  • id (string) – the ID of the message
  • body (string) – the raw message body
  • timestamp (int) – the timestamp the message was produced
  • attempts (int) – the number of times this message was attempted
Variables:
  • id – the ID of the message (from the parameter).
  • body – the raw message body (from the parameter).
  • timestamp – the timestamp the message was produced (from the parameter).
  • attempts – the number of times this message was attempted (from the parameter).
enable_async()

Enables asynchronous processing for this message.

nsq.Reader will not automatically respond to the message upon return of message_handler.

finish()

Respond to nsqd that you’ve processed this message successfully (or would like to silently discard it).

has_responded()

Returns whether or not this message has been responded to.

is_async()

Returns whether or not asynchronous processing has been enabled.

off(name, callback)

Stop listening for the named event via the specified callback.

Parameters:
  • name (string) – the name of the event
  • callback (callable) – the callback that was originally used
on(name, callback)

Listen for the named event with the specified callback.

Parameters:
  • name (string) – the name of the event
  • callback (callable) – the callback to execute when the event is triggered
requeue(**kwargs)

Respond to nsqd that you’ve failed to process this message successfully (and would like it to be requeued).

Parameters:
  • backoff (bool) – whether or not nsq.Reader should apply backoff handling
  • delay (int) – the amount of time (in seconds) that this message should be delayed if -1 it will be calculated based on # of attempts
touch()

Respond to nsqd that you need more time to process the message.

trigger(name, *args, **kwargs)

Execute the callbacks for the listeners on the specified event with the supplied arguments.

All extra arguments are passed through to each callback.

Parameters:name (string) – the name of the event