twilio-python icon indicating copy to clipboard operation
twilio-python copied to clipboard

Is this library thread-safe?

Open wojcikstefan opened this issue 3 years ago • 3 comments

I haven't seen anything in your documentation, code, nor old GH issues that would explicitly say whether this library is or isn't thread-safe (and whether that's even the goal). I think it would be good to address that, especially since sending multiple concurrent requests to Twilio's API is a pretty common use case. A similar question has already been asked & answered for your Java client (https://github.com/twilio/twilio-java/issues/441).

Based on my own (admittedly shallow) investigation, using your Python client may not be thread-safe because:

  1. By default, TwilioHttpClient uses requests.Session and there's still an open issue discussing whether that is thread-safe or not.
  2. Using TwilioHttpClient.last_request/last_response is definitely not thread-safe.

wojcikstefan avatar Nov 16 '21 07:11 wojcikstefan

Your analysis is correct. Until the thread-safety of requests.Session can be proven then the thread-safety of the client here cannot made. For last_request/response, these are intended for testing purposes only and should not be relied upon in a production set up.

childish-sambino avatar Feb 18 '22 17:02 childish-sambino

Your analysis is correct. Until the thread-safety of requests.Session can be proven then the thread-safety of the client here cannot made.

Alright, thanks for confirming my suspicions!

For last_request/response, these are intended for testing purposes only and should not be relied upon in a production set up.

Good to know! It would be great to document that in your code, e.g. via a comment above where these fields are first mentioned or via a more obvious variable name, like _test_only_last_request.

wojcikstefan avatar Feb 18 '22 17:02 wojcikstefan

Thanks for the documentation suggestion @wojcikstefan! It's been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

beebzz avatar Feb 25 '22 22:02 beebzz