[Python-Dev] Investigating time for `import requests`
raymond.hettinger at gmail.com
Mon Oct 2 05:42:19 EDT 2017
> On Oct 2, 2017, at 12:39 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> "What requests uses" can identify a useful set of
> avoidable imports. A Flask "Hello world" app could likely provide
> another such sample, as could some example data analysis notebooks).
Right. It is probably worthwhile to identify which parts of the library are typically imported but are not ever used. And likewise, identify a core set of commonly used tools that are going to be almost unavoidable in sufficiently interesting applications (like using requests to access a REST API, running a micro-webframework, or invoking mercurial).
Presumably, if any of this is going to make a difference to end users, we need to see if there is any avoidable work that takes a significant fraction of the total time from invocation through the point where the user first sees meaningful output. That would include loading from nonvolatile storage, executing the various imports, and doing the actual application.
I don't expect to find anything that would help users of Django, Flask, and Bottle since those are typically long-running apps where we value response time more than startup time.
For scripts using the requests module, there will be some fruit because not everything that is imported is used. However, that may not be significant because scripts using requests tend to be I/O bound. In the timings below, 6% of the running time is used to load and run python.exe, another 16% is used to import requests, and the remaining 78% is devoted to the actual task of running a simple REST API query. It would be interesting to see how much of the 16% could be avoided without major alterations to requests, to urllib3, and to the standard library.
For mercurial, "hg log" or "hg commit" will likely be instructive about what portion of the imports actually get used. A push or pull will likely be I/O bound so those commands are less informative.
--------- Quick timing for a minimal script using the requests module -----------
$ cat > demo_github_rest_api.py
info = requests.get('https://api.github.com/users/raymondh').json()
print('%(name)s works at %(company)s. Contact at %(email)s' % info)
$ time python3.6 demo_github_rest_api.py
Raymond Hettinger works at SauceLabs. Contact at None
$ time python3.6 -c "import requests"
$ time python3.6 -c ""
More information about the Python-Dev