问题描述
我已经在 virtualenv 中安装了 pytz(v2013.8,但它发生在 2013.b、2011k).第一次调用
I have installed pytz (v2013.8, but it happens in 2013.b, 2011k) in a virtualenv. The first call to
pytz.timezone("US/Eastern")
大约需要 4 秒.在常规环境中,这基本上是瞬时的.
takes about 4 seconds. In a regular environment this is essentially instantaneous.
有没有人有办法让它运行得更快?
Does anyone have a trick to get this to run faster?
推荐答案
我实际上是通过四处看看源代码来找到答案的.由于它从 egg 中获取时区设置,并且第一次调用 timezone 必须检查所有时区文件是否存在,因此第一次调用可能会很慢,具体取决于操作系统如何找到这些文件.如果 pytz 是使用 apt-get install python-tz
安装的,那么调用会命中未压缩的文件并且速度非常快.如果它是使用 easy_install pytz
安装的,那么它会一遍又一遍地点击一个压缩文件,速度会更慢.
I actually came across the answer by playing around and looking at the source code. Since it gets its timezone settings from within the egg and the first call to timezone has to check that all the timezone files exist, the first call could be slow depending on how the os has to find those files. If pytz is installed using apt-get install python-tz
then then call hits uncompressed files and is very fast. If it is installed using easy_install pytz
then it hits one compressed file over and over again and is slower.
所以解决办法是解压.幸运的是 pip 有一个方便的命令.
So the solution is to uncompress it. Luckily pip has a handy command.
tl;博士
pip unzip pytz
这篇关于第一次调用 pytz.timezone 在 virtualenv 中很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!