Coder Social home page Coder Social logo

Request Timing about requests-futures HOT 3 CLOSED

ross avatar ross commented on August 19, 2024
Request Timing

from requests-futures.

Comments (3)

ross avatar ross commented on August 19, 2024

something like the following should do the trick for something quick and dirty.

#!/usr/bin/env python

from datetime import datetime
from requests_futures.sessions import FuturesSession

session = FuturesSession()
times = {}

def time_it(sess, resp):
    times[resp.url] = datetime.now() - times[resp.url]

def do_request(url):
    times[url] = datetime.now()
    return session.get(url, background_callback=time_it)

futures = []
for url in ('http://httpbin.org/get', 'http://httpbin.org/get?key=val',
            'http://httpbin.org/get?key2=val2'):
    futures.append(do_request(url))
for future in futures:
    future.result() # you'd want to actually do something with the result here
# now that we've waited for everything times should be the durations
for url, delta in times.items():
    dur = delta.days * 24 * 60 * 60 * 1000 + delta.seconds * 1000 + \
        delta.microseconds / 1000.0
    print '%4dms %s' % (dur, url)

if i were doing it in something real/perm i'd probably inherit from FuturesSession and build/add the timing logic in to the request method. something like.

#!/usr/bin/env python

from datetime import datetime
from requests_futures.sessions import FuturesSession

class TimingSession(FuturesSession):

    def __init__(self, *args, **kwargs):
        super(TimingSession, self).__init__(*args, **kwargs)
        self.timing = {}

    def request(self, method, url, *args, **kwargs):
        background_callback = kwargs.pop('background_callback', None)

        # start counting
        self.timing[url] = datetime.now()

        def time_it(sess, resp):
            # here if you want to time the server stuff only
            self.timing[resp.url] = datetime.now() - self.timing[resp.url]
            if background_callback:
                background_callback(sess, resp)
            # here if you want to include any time in the callback

        return super(TimingSession, self).request(method, url, *args,
                                                  background_callback=time_it,
                                                  **kwargs)


session = TimingSession()

futures = []
for url in ('http://httpbin.org/get', 'http://httpbin.org/get?key=val',
            'http://httpbin.org/get?key2=val2'):
    futures.append(session.get(url))
for future in futures:
    future.result()
for url, delta in session.timing.items():
    dur = delta.days * 24 * 60 * 60 * 1000 + delta.seconds * 1000 + \
        delta.microseconds / 1000.0
    print '%4dms %s' % (dur, url)

from requests-futures.

michaeldnelson avatar michaeldnelson commented on August 19, 2024

Wow! Thanks for the quick detailed answer. This will really help me out.

from requests-futures.

ross avatar ross commented on August 19, 2024

np

from requests-futures.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.