Skip to main content

Wrapper for pulsar-client that publishes messages asynchronously and handles consumed messages in an asyncio event loop

Project description

Pulsar AsyncIO Client

Wrapper for pulsar-client that publishes messages asynchronously and handles consumed messages in an asyncio event loop.

Installation

$ pip install pulsar-asyncio-client

Usage

import asyncio
import pulsar_asyncio

PULSAR_URL = 'pulsar://localhost:6650'
PULSAR_TOPIC = 'non-persistent://public/default/my-topic'
PULSAR_SUBSCRIPTION = 'my-sub'

async def handle_message_async(consumer, message):
    message_text = message.data().decode('utf-8')
    print(f"Started asynchronously handling message: {message_text}")
    await asyncio.sleep(0.1)
    print(f"Finished asynchronously handling message: {message_text}")
    consumer.acknowledge(message)

async def main():
    # Init Pulsar client
    client = pulsar_asyncio.Client(PULSAR_URL)
    
    # Start a consumer with an async message listener
    consumer = client.subscribe(
        PULSAR_TOPIC,
        subscription_name=PULSAR_SUBSCRIPTION,
        message_listener=handle_message_async
    )

    # Asynchronously publish some messages
    producer = await client.create_producer(PULSAR_TOPIC)
    for i in range(10):
        message_text = f"My Message #{i}"
        print(f"About to asynchronously publish message: {message_text}")
        await producer.send(message_text.encode('utf-8'))

    # Allow some time for all messages to be consumed
    await asyncio.sleep(1)

    # Stop consumer
    consumer.close()
    
    # Stop client
    await client.close()

if __name__ == '__main__':
    asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pulsar_asyncio_client-0.1.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pulsar_asyncio_client-0.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file pulsar_asyncio_client-0.1.0.tar.gz.

File metadata

  • Download URL: pulsar_asyncio_client-0.1.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for pulsar_asyncio_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3f4555819c33643db9278495c3a693229d06bc6f6eea9012ca6a51625d8f4fa3
MD5 853bdf344f44a3ada0c3cade2a44686a
BLAKE2b-256 a02e7886c1398f36ba12cc0f1db568b79bc6f4c1eb95876f4206353c384e87ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for pulsar_asyncio_client-0.1.0.tar.gz:

Publisher: build-and-publish.yaml on U-238/pulsar-asyncio-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pulsar_asyncio_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pulsar_asyncio_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dcec5b12ef2d07dad5ac5e446e95bb260588bc01982d99bb7b1726ede476c930
MD5 a6daab6f9cc91f73317e2a6ae6ea54b0
BLAKE2b-256 b2dffbd98003c134269313dbc207d06b7154302cc483f63a35ad156b0be03c52

See more details on using hashes here.

Provenance

The following attestation bundles were made for pulsar_asyncio_client-0.1.0-py3-none-any.whl:

Publisher: build-and-publish.yaml on U-238/pulsar-asyncio-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page