stripe-python: Stripe import is very slow
Describe the bug
I looked around, searched the issues and even googled. Surprised this hasn’t come up.
Just importing the library adds 500ms to startup time. There’s got to be a way to lazy load parts.
This is on a top of the line Mac with SSD.
To Reproduce
# Takes about 20 ms
time python -c "print('ok')";
# Takes bout 500ms
time python -c "print('ok'); import stripe"
Expected behavior
For serverless setups, startup time is pretty important.
Even for local development, django’s runserver or uvicorn’s reload are both much slower with imports like these.
Of course you can do the import locally everywhere stripe is used, but this can be problematic.
At the very least, some guidance in the docs on how to do imports and have a lazy-loaded client would help. Something like a central get_stripe_client() -> Stripe or the like. And cover thread-safety in here.
Code snippets
No response
OS
macOS
Language version
3.12.1
Library version
11.3.0
API version
2020-08-27
Additional context
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 3
- Comments: 17 (2 by maintainers)
Over 50% of start time for our app is by the stripe SDK, and like others have observed, RAM usage has crept up as the library version has matured
Upgraded from an old version and got a major increase of memory usage. Tried several versions from major 9 to 12. None seemed to be fixed.
When can a fixed version be expected?
For production, yes, it’s mostly an issue with cold starts. If you use python’s
importtimeyou can see Stripe takes up a big, big chunk. See more here: https://adamj.eu/tech/2023/03/02/django-profile-and-improve-import-time/Cold start platforms: Google Cloud Run in my case.
But it also has local dev implications. Django and FastAPI rely on an autoreloading server. Unlike frontend tools like vite, there’s no concept of hot module reloading. So this library — with no usage, just importing — is responsible for a 500ms lag in restarts.
Imagine a few other libraries like this (Google storage does the same thing), and it starts to add up.
Can confirm the memory usage has been an issue for us as well. We ended up refactoring out the stripe-python sdk. Claude makes short work out of building your own stripe.construct_event() method, dropped 50mb per worker from our memory use.
I took a peek with memray. I see that it imports both aiohttp and requests, lovely. Those were installed in my testing environment. _test_helpers_service looks questionable for importing. https://gitlab.com/-/snippets/4802529