-
Notifications
You must be signed in to change notification settings - Fork 9
Add asynchronous interface #86
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
Merged
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e5fc783
add asyncio wrapper classes
dadodimauro 156aaf6
handle connection close with callbacks
dadodimauro 78db3e7
edit publisher sender handling
dadodimauro 10b988c
safer handling in close
dadodimauro 18e80d6
ignore amqp:resource-deleted when closing management sender and receive
dadodimauro 5320813
fix formatting
dadodimauro d440429
add tests for async publisher
dadodimauro 7c45ec0
fix publisher tests
dadodimauro 9510cf1
add tests for async consumer
dadodimauro 0b8fb2e
add tests for async management
dadodimauro 2835843
add tests for async connection and format
dadodimauro aec72f6
add tests for amqp 0.9.1
dadodimauro 2f0736d
add tests for streams with async classes
dadodimauro 96f74fd
Update README with fix for SSLUnavailable error
dadodimauro e3a397e
fix type hint uncompatible with python 3.9
dadodimauro a0ee953
better handling of asyncio.Lock and event loop, add docstrings
dadodimauro 83a7cb8
minor fix
dadodimauro 331aebf
add async getting started example
dadodimauro 8187a18
add type: ignore to example
dadodimauro 9dc2bed
fix async consumer
dadodimauro 2c63373
add more async examples
dadodimauro 23cd38d
update readme
dadodimauro 2bb1443
fix black errors for examples
dadodimauro 1bba34f
fix problems after copilot review
dadodimauro 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
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
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,149 @@ | ||
| # type: ignore | ||
|
|
||
| import asyncio | ||
| import signal | ||
|
|
||
| from rabbitmq_amqp_python_client import ( | ||
| AddressHelper, | ||
| AMQPMessagingHandler, | ||
| AsyncEnvironment, | ||
| Converter, | ||
| Event, | ||
| ExchangeSpecification, | ||
| ExchangeToQueueBindingSpecification, | ||
| Message, | ||
| OutcomeState, | ||
| QuorumQueueSpecification, | ||
| ) | ||
|
|
||
| MESSAGES_TO_PUBLISH = 100 | ||
|
|
||
|
|
||
| class StopConsumerException(Exception): | ||
| """Exception to signal consumer should stop""" | ||
|
|
||
| pass | ||
|
|
||
|
|
||
| class MyMessageHandler(AMQPMessagingHandler): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self._count = 0 | ||
|
|
||
| def on_amqp_message(self, event: Event): | ||
| print( | ||
| "received message: {} ".format( | ||
| Converter.bytes_to_string(event.message.body) | ||
| ) | ||
| ) | ||
|
|
||
| self.delivery_context.accept(event) | ||
| self._count = self._count + 1 | ||
| print("count " + str(self._count)) | ||
|
|
||
| def on_connection_closed(self, event: Event): | ||
| print("connection closed") | ||
|
|
||
| def on_link_closed(self, event: Event) -> None: | ||
| print("link closed") | ||
|
|
||
|
|
||
| async def main(): | ||
| exchange_name = "test-exchange" | ||
| queue_name = "example-queue" | ||
| routing_key = "routing-key" | ||
|
|
||
| print("connection to amqp server") | ||
| async with AsyncEnvironment( | ||
| uri="amqp://guest:guest@localhost:5672/" | ||
| ) as environment: | ||
| async with await environment.connection() as connection: | ||
| async with await connection.management() as management: | ||
| print("declaring exchange and queue") | ||
| await management.declare_exchange( | ||
| ExchangeSpecification(name=exchange_name) | ||
| ) | ||
| await management.declare_queue( | ||
| QuorumQueueSpecification(name=queue_name) | ||
| ) | ||
|
|
||
| print("binding queue to exchange") | ||
| bind_name = await management.bind( | ||
| ExchangeToQueueBindingSpecification( | ||
| source_exchange=exchange_name, | ||
| destination_queue=queue_name, | ||
| binding_key=routing_key, | ||
| ) | ||
| ) | ||
|
|
||
| addr = AddressHelper.exchange_address(exchange_name, routing_key) | ||
| addr_queue = AddressHelper.queue_address(queue_name) | ||
|
|
||
| print("create a publisher and publish a test message") | ||
| async with await connection.publisher(addr) as publisher: | ||
| print("purging the queue") | ||
| messages_purged = await management.purge_queue(queue_name) | ||
| print("messages purged: " + str(messages_purged)) | ||
|
|
||
| # publish messages | ||
| for i in range(MESSAGES_TO_PUBLISH): | ||
| status = await publisher.publish( | ||
| Message( | ||
| body=Converter.string_to_bytes( | ||
| "test message {} ".format(i) | ||
| ) | ||
| ) | ||
| ) | ||
| if status.remote_state == OutcomeState.ACCEPTED: | ||
| print("message accepted") | ||
|
|
||
| print( | ||
| "create a consumer and consume the test message - press control + c to terminate" | ||
| ) | ||
| handler = MyMessageHandler() | ||
| async with await connection.consumer( | ||
| addr_queue, message_handler=handler | ||
| ) as consumer: | ||
| # Create stop event and signal handler | ||
| stop_event = asyncio.Event() | ||
|
|
||
| def handle_sigint(): | ||
| print("\nCtrl+C detected, stopping consumer gracefully...") | ||
| stop_event.set() | ||
|
|
||
| # Register signal handler | ||
| loop = asyncio.get_running_loop() | ||
| loop.add_signal_handler(signal.SIGINT, handle_sigint) | ||
|
|
||
| try: | ||
| # Run consumer in background | ||
| consumer_task = asyncio.create_task(consumer.run()) | ||
|
|
||
| # Wait for stop signal | ||
| await stop_event.wait() | ||
|
|
||
| # Stop consumer gracefully | ||
| print("Stopping consumer...") | ||
| await consumer.stop_processing() | ||
|
|
||
| # Wait for task to complete | ||
| try: | ||
| await asyncio.wait_for(consumer_task, timeout=3.0) | ||
| except asyncio.TimeoutError: | ||
| print("Consumer task timed out") | ||
|
|
||
| finally: | ||
| loop.remove_signal_handler(signal.SIGINT) | ||
|
|
||
| print("unbind") | ||
| await management.unbind(bind_name) | ||
|
|
||
| print("delete queue") | ||
| await management.delete_queue(queue_name) | ||
|
|
||
| print("delete exchange") | ||
| await management.delete_exchange(exchange_name) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
Oops, something went wrong.
Oops, something went wrong.
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.