One month down in 2025: How are your resolutions coming along? Check out how to get back on track here.
Forum Discussion
Carl HR
2 years agoHelpful | Level 6
Is the python dropbox api thread safe?
I'm using different dropbox contexts created with dropbox.Dropbox(...). In my application, I have two or more contexts sharing the same target account (and Console Apps) at a time. Example: contexts c1, c2 and c3 use the account foo@mail.com (App Foo) and c4, c5 use the account asd@mail.com (App Asd).
Can I use all of them in different threads (one context for a corresponding thread), assuming they will execute in parallel and will work as expected? Is there any need to use mutexes somewhere (in the case which the contexts that share the same account execute at the same time)?
I tried searching about the term "thread" inside the python dropbox docs in case I would find anything related, but found nothing about it. There's no info about the API being thread-safe anywhere on the site, or similar ones.
As I can see in the dropbox endpoints v2 page, all operations are be performed by using curl. Should I expect that the python's dropbox api also uses curl internally? Or should I use a python libcurl directly in this case, skipping the dropbox api entirely?
By searching in any python libcurl (such as PycURL, for example), it states that these libraries should be thread safe, which is exactly what I want, but if there's any chance to not have to reinvent the wheel, I would take it.
Thanks,
Carl HR.
- Greg-DB
Dropbox Staff
I don't believe we currently have any documented guarantee as to whether or not the Python SDK itself is thread-safe, so I can't make assurances on that, but I'll pass this along as a request to the team for that. I can't promise if or when that might be done though.
For reference though, the Dropbox Python SDK uses the requests library to perform the network requests to the Dropbox API. The Dropbox API servers will automatically take locks on resources as needed, e.g., on "namespaces". Check out this guide for more information on that server-side behavior.
- Carl HRHelpful | Level 6
Turns out that this answer is not very easy to answer, as even the requests module don't assure wether their api is thread safe.
Going as far to search on stackoverflow and reading the requests module source code , I found out that:
1. Sharing a single session instance on multiple threads is not thread-safe. However, apparently using one instance per thread is. So, in order to use this api correctly, we'd theoretically need to create different dropbox sessions per thread, in order to run code with thread safety.
2. Most of the thread-safety of the requests.sessions module depends on wether the urllib3 module is thread-safe. From current records, it turns out that the v2.0 is thread-safe, as seen on this GitHub thread, and on the urllib official docs .
Though imho its best to stick with option #1 as it's more reliable.
- ЗдравкоLegendary | Level 20
Hi Carl HR,
I think, you have misinterpreted the topics you have read. The thread safety there is evaluated in regards of ability to transfer session state between threads (something not guaranteed while using default Python requests library). When everything (Dropbox API call needs) is set explicitly, you don't need to care about any loss of state (no state - there is nothing to loss). The underlying library (urllib3, by the way no curl or some fork of libcurl otherwise available too), that carry the actual request, is thread safe. 😉 You have everything you need with one exception - don't share Dropbox client object between threads. The behavior on refresh would be undefined (something easy correctable with redefining 'check_and_refresh_access_token' - enclose the body in mutex).
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,950 PostsLatest Activity: 14 hours ago
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!