pynsq Documentation

repository·master·Indexed 19 days ago

https://github.com/nsqio/pynsq

The official Python client library for NSQ. It provides high-level abstractions via nsq.Reader and nsq.Writer for producers and consumers, as well as low-level modules like async_conn for direct protocol communication. Features include the nsq.AsyncConn class for asyncio patterns, a Client class for connection management, and the nsq.run() utility for consumer execution.

Tokens
1.3K
Snippets
5
Records
11
Agent score
66%

What's inside pynsq

  1. Overview of pynsq capabilities

    master

    pynsq is the official Python client library for NSQ. It offers two levels of abstraction for interacting with the NSQ protocol:

    1. High-level API: Use the nsq.Reader and nsq.Writer classes to build robust consumers and producers with minimal boilerplate.
    2. Low-level API: Use the async_conn module for synchronous or asynchronous communication directly over the NSQ TCP protocol. This is intended for developers who need to implement custom high-level functionality.

    Note on Dependencies: If you use the async_conn module, you must have tornado installed, as the async module is built on top of the Tornado IOLoop.

  2. Use the Writer class to produce messages

    master
    The nsq.Writer class is a high-level producer used to send messages to an NSQ topic. It abstracts the underlying connection logic, allowing you to publish data to specific topics easily.
  3. Use the Reader class for high-level NSQ consumption

    master

    The nsq.Reader class is the high-level consumer interface for interacting with NSQ. It provides an abstraction for connecting to an NSQ network, subscribing to topics, and consuming messages from channels. Use this class to manage the lifecycle of a consumer, including starting and stopping message processing.

    import nsq
    
    # Example usage of the Reader class
    reader = nsq.Reader(topic='my_topic', channel='my_channel', handlers=[my_handler])
    reader.connect()
    # ... process messages ...
    reader.stop()
  4. Run an NSQ consumer with nsq.run()

    master

    The nsq.run() function is a convenience utility designed to simplify the execution of an NSQ consumer. It handles the setup and lifecycle management of the reader, making it suitable for simple scripts or entry points where you want to start consuming messages and keep the process running until interrupted.

    import nsq
    
    def my_handler(message):
        print(f'Received message: {message.body}')
        message.finish()
    
    # Use run() to start the consumer loop
    nsq.run(topic='my_topic', channel='my_channel', handlers=[my_handler])
  5. Use AsyncConn for asynchronous connections to nsqd

    master
    The nsq.AsyncConn class provides an asynchronous connection to an nsqd instance. It is the primary interface for interacting with NSQ using Python's asyncio patterns. Use this class when you need to perform non-blocking operations such as publishing messages or managing topic/channel subscriptions in an asynchronous environment.
  6. Use LegacyReader for backwards compatibility

    master
    The nsq.LegacyReader class provides a backwards compatible interface for reading from NSQ. It is intended for users who need to maintain compatibility with older versions of the NSQ client or existing codebases that rely on the legacy reader pattern.
  7. Use the Client class to manage NSQ connections

    master

    The Client class is the primary entrypoint for managing connections to NSQ servers. It runs on the current Tornado IOLoop and includes a background periodic callback that checks for stale connections every 60 seconds. A connection is considered stale if it hasn't received data for more than twice its configured heartbeat_interval.

    from nsq.client import Client
    
    # Initialize the client
    # Note: Client requires a running Tornado IOLoop
    client = Client(name='my_client')
  8. Use the Message class to handle NSQ messages

    master
    The nsq.Message class represents an individual message received from an NSQ topic/channel. It contains the message payload and metadata required for processing and acknowledging the message.
  9. Override heartbeat() to monitor connection liveness

    master

    The heartbeat(conn) method is a hook provided by the Client class. It is called automatically whenever a heartbeat is received from an NSQ server. You can subclass Client and override this method to perform custom actions, such as updating monitoring metrics or logging liveness status.

    :param conn: The nsq.AsyncConn instance over which the heartbeat was received.

    from nsq.client import Client
    
    class MyMonitoringClient(Client):
        def heartbeat(self, conn):
            # Perform custom action based on liveness
            print(f"Heartbeat received on connection: {conn.id}")
            # Call super() if you want to maintain default behavior
            super().heartbeat(conn)
    
    client = MyMonitoringClient(name='monitor_client')