Closed cabal-daniel closed 4 months ago
Thank you for your feedback. Tagging and routing to the team member best able to assist.
Hi @cabal-daniel,
our typical usage pattern is to use the context manager ( with ) or simply managing the opening / closing of the client by yourself, so the code ends up like this.
It shouldn't impact anything but you will have sockets left open, other pieces of code that might be running. In general it is a good idea to call close just in case.
from azure.identity import DefaultAzureCredential
from azure.servicebus import ServiceBusClient, ServiceBusMessage
from datetime import datetime, timedelta
client = ServiceBusClient(
fully_qualified_namespace='foobar',
credential=DefaultAzureCredential(),
)
start = datetime.now()
sender_context = client.get_queue_sender(queue_name='hello')
checkpoint_1 = datetime.now()
sender.send_messages(ServiceBusMessage('hi')) # this will open up the connection if not open
checkpoint_2 = datetime.now()
sender = sender_context.close()
checkpoint_3 = datetime.now()
Hi @cabal-daniel. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.
@kashifkhan the problem is that opening the connection and closing the connection takes a considerable amount of time
initiating took 1255.869ms
sending took 77.747ms
closing took 804.139ms
For our pods, it seems easier to just keep the connection open and detect if the connection is closed on the other end then re-open so sending messages takes 77ms rather than 3000ms if we're using SB to build a DAG.
@cabal-daniel thank you for that context. There are some things to note here
DefaultAzureCredential
can add some overhead as it tries to determine which credential to use. That itself should save you some time on connectssend_messages
will take some time as it has to open a connection, while subsequent calls will be fast as the connection is already open. __exit__
is the same as sender.close()
, so there is no short circuit here.@kashifkhan thank you for this helpful information.
Could you comment on the following type of architecture for distributed compute?
atexit
library to close all the connectionsBetween steps 2 to 5, the workers could be alive for days or weeks.
@cabal-daniel
In general the strategy looks fine, just wanted to add a few notes:
get_queue_sender
/get_topic_sender
wont open the connection. An operation such as send_messages
will do that and then subsequent calls will be faster as they will benefit from an open connection.DefaultAzureCredential
to the exact credential you are looking for such as ManagedIdentityCredential
etc will speed up first connection time.Got it -- thanks @kashifkhan ! That makes sense.
Consider the following code
The results are
If a process never calls
sender_context.__exit__()
because the process gets SIGTERM-ed, would that cause problems?