-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add sync/async samples to demonstrate consuming from a number of sessions at one time. #11001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
KieranBrantnerMagee
merged 2 commits into
Azure:master
from
KieranBrantnerMagee:kibrantn/servicebus/track2-session-pool-samples
Apr 27, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
sdk/servicebus/azure-servicebus/samples/async_samples/session_pool_receive_async.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import asyncio | ||
| import uuid | ||
|
|
||
| from azure.servicebus.aio import ServiceBusClient, AutoLockRenew | ||
| from azure.servicebus import NoActiveSession, Message | ||
|
|
||
|
|
||
| CONNECTION_STR = os.environ['SERVICE_BUS_CONNECTION_STR'] | ||
| # Note: This must be a session-enabled queue. | ||
| QUEUE_NAME = os.environ["SERVICE_BUS_QUEUE_NAME"] | ||
|
|
||
| async def message_processing(servicebus_client, queue_name): | ||
| while True: | ||
| try: | ||
| async with servicebus_client.get_queue_session_receiver(queue_name, idle_timeout=1) as receiver: | ||
| renewer = AutoLockRenew() | ||
| renewer.register(receiver.session, timeout=None) | ||
| await receiver.session.set_session_state("OPEN") | ||
| async for message in receiver: | ||
| print("Message: {}".format(message)) | ||
| print("Time to live: {}".format(message.header.time_to_live)) | ||
| print("Sequence number: {}".format(message.sequence_number)) | ||
| print("Enqueue Sequence numger: {}".format(message.enqueue_sequence_number)) | ||
| print("Partition ID: {}".format(message.partition_id)) | ||
| print("Partition Key: {}".format(message.partition_key)) | ||
| print("Locked until: {}".format(message.locked_until_utc)) | ||
| print("Lock Token: {}".format(message.lock_token)) | ||
| print("Enqueued time: {}".format(message.enqueued_time_utc)) | ||
| await message.complete() | ||
| if str(message) == 'shutdown': | ||
| await receiver.session.set_session_state("CLOSED") | ||
| break | ||
| renewer.shutdown() | ||
| except NoActiveSession: | ||
| print("There are no non-empty sessions remaining; exiting. This may present as a UserError in the azure portal.") | ||
| return | ||
|
|
||
|
|
||
| async def sample_session_send_receive_with_pool_async(connection_string, queue_name): | ||
|
|
||
| concurrent_receivers = 5 | ||
| sessions = [str(uuid.uuid4()) for i in range(concurrent_receivers)] | ||
| client = ServiceBusClient.from_connection_string(connection_string) | ||
|
|
||
| for session_id in sessions: | ||
| async with client.get_queue_sender(queue_name) as sender: | ||
| await asyncio.gather(*[sender.send(Message("Sample message no. {}".format(i), session_id=session_id)) for i in range(20)]) | ||
| await sender.send(Message("shutdown", session_id=session_id)) | ||
|
|
||
| receive_sessions = [message_processing(client, queue_name) for _ in range(concurrent_receivers)] | ||
| await asyncio.gather(*receive_sessions) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| loop = asyncio.get_event_loop() | ||
| loop.run_until_complete(sample_session_send_receive_with_pool_async(CONNECTION_STR, QUEUE_NAME)) |
68 changes: 68 additions & 0 deletions
68
sdk/servicebus/azure-servicebus/samples/sync_samples/session_pool_receive.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import uuid | ||
| import concurrent | ||
|
|
||
| from azure.servicebus import ServiceBusClient, Message, AutoLockRenew | ||
| from azure.servicebus import NoActiveSession | ||
|
|
||
| CONNECTION_STR = os.environ['SERVICE_BUS_CONNECTION_STR'] | ||
| # Note: This must be a session-enabled queue. | ||
| QUEUE_NAME = os.environ["SERVICE_BUS_QUEUE_NAME"] | ||
|
|
||
| def message_processing(sb_client, queue_name, messages): | ||
| while True: | ||
| try: | ||
| with sb_client.get_queue_session_receiver(queue_name, idle_timeout=1) as receiver: | ||
| renewer = AutoLockRenew() | ||
| renewer.register(receiver.session, timeout=None) | ||
| receiver.session.set_session_state("OPEN") | ||
| for message in receiver: | ||
| messages.append(message) | ||
| print("Message: {}".format(message)) | ||
| print("Time to live: {}".format(message.header.time_to_live)) | ||
| print("Sequence number: {}".format(message.sequence_number)) | ||
| print("Enqueue Sequence numger: {}".format(message.enqueue_sequence_number)) | ||
| print("Partition ID: {}".format(message.partition_id)) | ||
| print("Partition Key: {}".format(message.partition_key)) | ||
| print("Locked until: {}".format(message.locked_until_utc)) | ||
| print("Lock Token: {}".format(message.lock_token)) | ||
| print("Enqueued time: {}".format(message.enqueued_time_utc)) | ||
| message.complete() | ||
| if str(message) == 'shutdown': | ||
| receiver.session.set_session_state("CLOSED") | ||
| renewer.shutdown() | ||
| except NoActiveSession: | ||
| print("There are no non-empty sessions remaining; exiting. This may present as a UserError in the azure portal.") | ||
| return | ||
|
|
||
|
|
||
| def sample_session_send_receive_with_pool(connection_string, queue_name): | ||
|
|
||
| concurrent_receivers = 5 | ||
| sessions = [str(uuid.uuid4()) for i in range(2 * concurrent_receivers)] | ||
| with ServiceBusClient.from_connection_string(connection_string) as client: | ||
|
|
||
| with client.get_queue_sender(queue_name) as sender: | ||
| for session_id in sessions: | ||
| for i in range(20): | ||
| message = Message("Sample message no. {}".format(i), session_id=session_id) | ||
| sender.send(message) | ||
|
|
||
| all_messages = [] | ||
| futures = [] | ||
| with concurrent.futures.ThreadPoolExecutor(max_workers=concurrent_receivers) as thread_pool: | ||
| for _ in range(concurrent_receivers): | ||
| futures.append(thread_pool.submit(message_processing, client, queue_name, all_messages)) | ||
| concurrent.futures.wait(futures) | ||
|
|
||
| print("Received total {} messages across {} sessions.".format(len(all_messages), len(sessions))) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| sample_session_send_receive_with_pool(CONNECTION_STR, QUEUE_NAME) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.