Note that there are some explanatory texts on larger screens.

plurals
  1. POPycurl: how to determine duration of a request
    primarykey
    data
    text
    <p>My task is to do periodic requests to server and write time of this request to database. I use Python 2.7.3, cURL and pycurl as wrapper for cURL. I do request like so:</p> <pre><code>import pycurl c = pycurl.Curl() c.setopt(c.URL, "http://google.com/") c.setopt(c.VERBOSE, True) # to see request details c.perform() </code></pre> <p>But how I can determine the time of this request?</p> <p><strong>UPD1</strong></p> <p>Ok, I understand, that I should take 2 timestamps and the diff between them will be the duration of request. But I face some problem:</p> <p>when I execute:</p> <pre><code>import pycurl, time c = pycurl.Curl() c.setopt(c.URL, "http://google.com/") c.setopt(c.VERBOSE, True) # to see request details ts = time.clock() c.perform() print time.clock() - ts </code></pre> <p>I get 0.0 o (sometimes) 0.01. It's wrong diff, because of I execute this commands in python shell and some time is left after I do <code>ts = time.clock()</code> and before I do <code>print time.clock() - ts</code>, so the diff should be about 3 sec.</p> <p>This output I get on my Ubuntu server 12.04 installed on Virtualbox. Virtualbox installed on Windows 7. When I try code below in windows 7 I get correct output.</p> <p>Here is another question - maybe I should use time.time() instead of time.clock()?</p> <p><strong>Solution</strong></p> <p>Idea was taken from <a href="http://coreygoldberg.blogspot.com/2008/09/python-timing-timeclock-vs-timetime.html" rel="nofollow">this post</a>. Succinctly, you should use time.clock() on windows and time.time() on Linux or Unix (Ubuntu and others, FreeBSD and others, MacOS). And you can also use timeit.default_timer(). This function detect os type and choose time.time() or time.clock(). So the solution's code is:</p> <pre><code>import pycurl, timeit c = pycurl.Curl() c.setopt(c.URL, "http://google.com/") c.setopt(c.VERBOSE, True) # to see request details ts = timeit.default_timer() c.perform() print timeit.default_timer() - ts </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload