-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding an event to catch connections issues
- Loading branch information
1 parent
3076c86
commit 6f58020
Showing
7 changed files
with
135 additions
and
15 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
docs/examples/check_connection_broken/consumer_handle_connections_issues.py
This file contains 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,41 @@ | ||
import asyncio | ||
import signal | ||
|
||
from rstream import ( | ||
AMQPMessage, | ||
Consumer, | ||
MessageContext, | ||
amqp_decoder, | ||
) | ||
|
||
STREAM = "my-test-stream" | ||
|
||
|
||
def on_connection_closed(reason: Exception) -> None: | ||
print("connection has been closed for reason: " + str(reason)) | ||
|
||
|
||
async def consume(): | ||
consumer = Consumer( | ||
host="localhost", | ||
port=5552, | ||
vhost="/", | ||
username="guest", | ||
password="guest", | ||
connection_closed_handler=on_connection_closed, | ||
) | ||
|
||
loop = asyncio.get_event_loop() | ||
loop.add_signal_handler(signal.SIGINT, lambda: asyncio.create_task(consumer.close())) | ||
|
||
async def on_message(msg: AMQPMessage, message_context: MessageContext): | ||
stream = message_context.consumer.get_stream(message_context.subscriber_name) | ||
offset = message_context.offset | ||
print("Got message: {} from stream {}, offset {}".format(msg, stream, offset)) | ||
|
||
await consumer.start() | ||
await consumer.subscribe(stream=STREAM, callback=on_message, decoder=amqp_decoder) | ||
await consumer.run() | ||
|
||
|
||
asyncio.run(consume()) |
34 changes: 34 additions & 0 deletions
34
docs/examples/check_connection_broken/producer_handle_connections_issues.py
This file contains 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,34 @@ | ||
import asyncio | ||
import time | ||
|
||
from rstream import AMQPMessage, Producer | ||
|
||
STREAM = "my-test-stream" | ||
MESSAGES = 1000000 | ||
|
||
|
||
def on_connection_closed(reason: Exception) -> None: | ||
print("connection has been closed for reason: " + str(reason)) | ||
|
||
|
||
async def publish(): | ||
|
||
async with Producer("localhost", username="guest", password="guest") as producer: | ||
# create a stream if it doesn't already exist | ||
await producer.create_stream(STREAM, exists_ok=True) | ||
|
||
# sending a million of messages in AMQP format | ||
start_time = time.perf_counter() | ||
|
||
for i in range(MESSAGES): | ||
amqp_message = AMQPMessage( | ||
body="hello: {}".format(i), | ||
) | ||
# send is asynchronous | ||
await producer.send(stream=STREAM, message=amqp_message) | ||
|
||
end_time = time.perf_counter() | ||
print(f"Sent {MESSAGES} messages in {end_time - start_time:0.4f} seconds") | ||
|
||
|
||
asyncio.run(publish()) |
This file contains 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 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 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 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 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