psf / requests

A simple, yet elegant, HTTP library.
https://requests.readthedocs.io/en/latest/
Apache License 2.0
52.16k stars 9.33k forks source link

"import requests" takes a full second? #2529

Closed hartwork closed 9 years ago

hartwork commented 9 years ago

Hi!

I noticed that the --help output of a tool of mine was noticably slower than that of my other tools. It took a full second to show up. I traced the lagging down to the call to import requests (and worked around it by importing requests only after processing command line options).

import requests takes at least a second on both the SSDed Debian and HDDed Gentoo, with version 2.5.3 as well as 2.6.0. Compared to the 30 milliseconds that other modules take to import, that's a long time:

# time python -c 'import requests; print requests.__file__'
/usr/lib64/python2.7/site-packages/requests/__init__.pyc

real    0m1.099s
user    0m1.055s
sys     0m0.039s

# time python -c 'import argparse; print argparse.__file__'
/usr/lib64/python2.7/argparse.pyc

real    0m0.030s
user    0m0.018s
sys     0m0.010s

It would be great if you could work on reducing import time. Many thanks!

kevinburke commented 9 years ago

I believe the slow part of importing requests is importing pyopenssl. In theory you could delay the import until the first time a request is made, though I'm pretty sure the team is not in favor of that approach.

hartwork commented 9 years ago

I believe the slow part of importing requests is importing pyopenssl.

That makes perfect sense:

# time python -c 'import OpenSSL.SSL; print OpenSSL.SSL.__file__'
/usr/lib64/python2.7/site-packages/OpenSSL/SSL.pyc

real    0m1.014s
user    0m0.991s
sys     0m0.018s
Lukasa commented 9 years ago

@hartwork Are you using a new virtualenv for each test? Mostly importing pyopenssl takes a while because cffi does an implicit C compile at import time in some cases, but that should only happen the first time.

hartwork commented 9 years ago

No virtualenv over here, no.

hartwork commented 9 years ago
# for i in {1..10} ; do sh -c "time python -c 'import OpenSSL.SSL'" |& sed -n '2,2p' ; done
real    0m1.004s
real    0m0.993s
real    0m0.996s
real    0m0.994s
real    0m0.997s
real    0m0.999s
real    0m0.998s
real    0m0.991s
real    0m0.999s
real    0m0.997s
Lukasa commented 9 years ago

Interesting.

Fundamentally, this is a cryptography bug, probably pyca/cryptography#1764. I'm afraid until that's resolved we cannot do anything about this.

afcady commented 7 years ago

On my little ARM system from NextThing this takes SEVEN SECONDS.

Lukasa commented 7 years ago

@afcady Please report the issue to the appropriate upstream.