[Python-Dev] Investigating time for `import requests`
k7hoven at gmail.com
Sun Oct 8 11:24:13 EDT 2017
On Sun, Oct 8, 2017 at 2:44 PM, Chris Angelico <rosuav at gmail.com> wrote:
> On Sun, Oct 8, 2017 at 7:02 PM, David Cournapeau <cournape at gmail.com>
> > It is certainly true that for a CLI tool that actually makes any network
> > I/O, especially SSL, import times will quickly be negligible. It becomes
> > tricky for complex tools, because of error management. For example, a
> > pattern I have used in the past is to have a high level "catch all
> > exceptions" function that dispatch the CLI command:
> > try:
> > main_function(...)
> > except ErrorKind1:
> > ....
> > except requests.exceptions.SSLError:
> > # gives complete message about options when receiving SSL errors,
> > invalid certificate
> > This pattern requires importing requests every time the command is run,
> > if no network IO is actually done. For complex CLI tools, maybe most
> > don't use network IO (the tool in question was a complete packages
> > but you pay ~100 ms because of requests import for every command. It is
> > particularly visible because commands latency starts to be felt around
> > 100-150 ms, and while you can do a lot in python in 100-150 ms, you
> can't do
> > much in 0-50 ms.
> This would be a perfect use-case for lazy importing, then. You'd pay
> the price of the import only if you get an error that isn't caught by
> one of the preceding except blocks.
I suppose it might be convenient to be able to do something like:
The easiest workaround at the moment is still pretty clumsy:
from requests.exceptions import SLLError
But what happens if that gives you an ImportError?
+ Koos Zevenhoven + http://twitter.com/k7hoven +
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-Dev