diff --git a/examples/pubsub-streaming-async/README.md b/examples/pubsub-streaming-async/README.md index dfa7d27d..60c1cdef 100644 --- a/examples/pubsub-streaming-async/README.md +++ b/examples/pubsub-streaming-async/README.md @@ -27,11 +27,11 @@ Run the following command in a terminal/command prompt: @@ -63,7 +63,7 @@ sleep: 15 ```bash # 2. Start Publisher -dapr run --app-id python-publisher --app-protocol grpc --dapr-grpc-port=3500 --enable-app-health-check python3 publisher.py +dapr run --app-id python-publisher --app-protocol grpc --dapr-grpc-port=3500 --enable-app-health-check -- python3 publisher.py --topic=TOPIC_B1 ``` @@ -75,11 +75,11 @@ Run the following command in a terminal/command prompt: @@ -111,7 +111,7 @@ sleep: 15 ```bash # 2. Start Publisher -dapr run --app-id python-publisher --app-protocol grpc --dapr-grpc-port=3500 --enable-app-health-check python3 publisher.py +dapr run --app-id python-publisher --app-protocol grpc --dapr-grpc-port=3500 --enable-app-health-check -- python3 publisher.py --topic=TOPIC_B2 ``` diff --git a/examples/pubsub-streaming-async/publisher.py b/examples/pubsub-streaming-async/publisher.py index b9702355..e4abf359 100644 --- a/examples/pubsub-streaming-async/publisher.py +++ b/examples/pubsub-streaming-async/publisher.py @@ -10,11 +10,18 @@ # See the License for the specific language governing permissions and # limitations under the License. # ------------------------------------------------------------ +import argparse import asyncio import json from dapr.aio.clients import DaprClient +parser = argparse.ArgumentParser(description='Publish events to a Dapr pub/sub topic.') +parser.add_argument('--topic', type=str, required=True, help='The topic name to publish to.') +args = parser.parse_args() + +topic_name = args.topic + async def publish_events(): """ @@ -30,7 +37,7 @@ async def publish_events(): # Create a typed message with content type and body await d.publish_event( pubsub_name='pubsub', - topic_name='TOPIC_A', + topic_name=topic_name, data=json.dumps(req_data), data_content_type='application/json', publish_metadata={'ttlInSeconds': '100', 'rawPayload': 'false'}, diff --git a/examples/pubsub-streaming-async/subscriber-handler.py b/examples/pubsub-streaming-async/subscriber-handler.py index f9503f06..34129ee7 100644 --- a/examples/pubsub-streaming-async/subscriber-handler.py +++ b/examples/pubsub-streaming-async/subscriber-handler.py @@ -1,7 +1,15 @@ +import argparse import asyncio from dapr.aio.clients import DaprClient from dapr.clients.grpc._response import TopicEventResponse +parser = argparse.ArgumentParser(description='Publish events to a Dapr pub/sub topic.') +parser.add_argument('--topic', type=str, required=True, help='The topic name to publish to.') +args = parser.parse_args() + +topic_name = args.topic +dlq_topic_name = topic_name + '_DEAD' + counter = 0 @@ -24,9 +32,9 @@ async def main(): # Subscribe to the pubsub topic with the message handler close_fn = await client.subscribe_with_handler( pubsub_name='pubsub', - topic='TOPIC_A', + topic=topic_name, handler_fn=process_message, - dead_letter_topic='TOPIC_A_DEAD', + dead_letter_topic=dlq_topic_name, ) # Wait until 5 messages are processed diff --git a/examples/pubsub-streaming-async/subscriber.py b/examples/pubsub-streaming-async/subscriber.py index 0f7da59b..9a0d34a5 100644 --- a/examples/pubsub-streaming-async/subscriber.py +++ b/examples/pubsub-streaming-async/subscriber.py @@ -1,8 +1,16 @@ +import argparse import asyncio from dapr.aio.clients import DaprClient from dapr.clients.grpc.subscription import StreamInactiveError +parser = argparse.ArgumentParser(description='Publish events to a Dapr pub/sub topic.') +parser.add_argument('--topic', type=str, required=True, help='The topic name to publish to.') +args = parser.parse_args() + +topic_name = args.topic +dlq_topic_name = topic_name + '_DEAD' + counter = 0 @@ -18,7 +26,7 @@ async def main(): async with DaprClient() as client: global counter subscription = await client.subscribe( - pubsub_name='pubsub', topic='TOPIC_A', dead_letter_topic='TOPIC_A_DEAD' + pubsub_name='pubsub', topic=topic_name, dead_letter_topic=dlq_topic_name ) try: