Comments (4)
@besfahbod How about this?
import pytz
def warm_pytz_cache() -> None:
for zone in pytz.all_timezones:
pytz.timezone(zone)
I'll note that if you migrate to zoneinfo
, the guarantees about the caching would require you to hold an actual reference to the objects you want to cache if you don't want to have a cache miss:
import zoneinfo
def warm_cache(*, _cache: dict[str: zoneinfo.ZoneInfo] = {}) -> None:
for zone in zoneinfo.available_timezones():
_cache[zone] = zoneinfo.ZoneInfo(zone)
That is guaranteed to work as part of the public interface by the PEP. I am not sure that pytz
makes any guarantees about its caching behavior, but my understanding is that development on pytz
is largely frozen, so I doubt they'll be re-working the cache system any time soon.
from pytz.
pytz won't throw away the timezone info once it is loaded, and @pganssle's warm_pytz_cache() method will work fine for most purposes. The only complication is when the database on disk has drifted, which could happen when pytz has been setup to use the system zoneinfo database (but unlikely; it requires a timezone to be completely removed from the database. To make this extremely unlikely, just warm common_timezones rather than all_timezones, so you don't try warming all the deprecated zones).
def warm_pytz_cache() -> None:
for zone in pytz.common_timezones:
try:
pytz.timezone(zone)
except Exception:
pass
Consider the above approach blessed, and I'll try to keep it working when transitioning to Python3.9's internals. I don't want to add a call to pytz to do this. I think it is an infrequent enough usage that use cases will be subtly different (warm all_timezones vs common_timezones for instance).
from pytz.
Thanks, @pganssle. The pytz
solution makes sense, and is in fact similar to what I have put in place in the application code right now. And, from observation, looks like the caching behavior is that it doesn't get cleared automatically, fwiw.
And thanks for the extra notes about the zoneinfo
caching behavior. Good to know when we migrate to zoneinfo
module.
Considering these, @pganssle, do you think it makes sense to add a specific API for these to pytz
(and probably suggest the same for zoneinfo
)?
from pytz.
Considering these, @pganssle, do you think it makes sense to add a specific API for these to
pytz
(and probably suggest the same forzoneinfo
)?
Can't speak for pytz
, but definitely not for zoneinfo
. The caching behavior is well-defined and I don't see a particularly strong justification for this, when it's pretty simple to write your own cache warmer if you really need it.
from pytz.
Related Issues (20)
- README links to (unmaintained?) Launchpad repos and bug tracker HOT 1
- tzinfo equality
- Tehran zone is +3:30 but in this package is +4:30 HOT 7
- `utcfromtimestamp()` is deprecated in Python 3.12
- Spelling error for Kyiv HOT 1
- Problem with timezone conversion when using pytz as tzinfo HOT 1
- DstTzInfo.localize() breaks for date '9999-12-31'
- RFE: is it possible to start making github releases?🤔 HOT 2
- Not handles if timezone given is False HOT 1
- Bad link in unofficial (?) documentation on sourceforge HOT 1
- Offsets in seconds are truncated HOT 1
- Any way to get DST rules and timezone shortname for both standard time and daylight savings time from pytz? HOT 1
- Bug/confusion of Etc/GMT timezones ?! HOT 4
- Incorrect Tz conversion adding extra minutes.
- Request: add link to GitHub repo to PyPi package metadata
- Long running application has started failing with "'_all_timezones_unchecked' is not defined" after recent upgrade HOT 1
- Invalid name of a specific timezone
- America/Mexico_City will not use DST anymore
- Wrong egypt timezone when using datetime replace HOT 1
- needs update with iana release version 2023b HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytz.