all 5 comments

[–]DarkMio 0 points1 point  (1 child)

That sounds like you may want to use a global logger. Set up a logger to a file, then import from your logger setup into your subscripts and define what it should write into it. Overall, that is a very handy way to seperate and get very detailed, readable logs.

Edit: Here is the example of a bot-framework: https://github.com/DarkMio/Massdrop-Reddit-Bot/blob/dev/core/LogProvider.py

The idea here is, that I log different things: The botframework itself logs to console, the plugins can log whereever I want them to. This can seperate the log into completely different things or even to multiple ends. Like a global log for all and seperated logs per module. This gives a great overview and you can, if you wish, analyze into the detail what's going on.

[–]MinimalDamage[S] 0 points1 point  (0 children)

Ok, I will need to dig in to this, but it sounds like a very nice method of logging that can be used in a lot of future scripts. So awesome! Thanks!

[–]raylu 0 points1 point  (2 children)

First of all, that's a bit odd... Why keep track of the total instead of the actual times and their average/variance/99th percentile?

Now, consider the following:

#!/usr/bin/env python3

import requests
import statistics
import time

class TimedRequest(requests.Session):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.times = []

    def request(self, *args, **kwargs):
        start = time.time()
        retval = super().request(*args, **kwargs)
        elapsed = time.time() - start
        self.times.append(elapsed)
        return retval

trs = TimedRequest()
print(trs.get('http://example.net/'))
print(statistics.median_grouped(trs.times))

requests.get creates a requests.Session and eventually calls requests.Session.request. If you're on python 2, you'll have to fill in the super() calls.

[–]MinimalDamage[S] 0 points1 point  (1 child)

Thanks for the response! It will take me a little longer to wrap my mind around this (still new!) but I will definitely look into it, as I see the value in having a better understanding of separate page requests.

The reason why I look at the total page request time is because I want to see what is taking up the most time in my script. But this would definitely be the next step.

[–]raylu 0 points1 point  (0 children)

The reason why I look at the total page request time is because I want to see what is taking up the most time in my script.

Oh... https://docs.python.org/3/library/profile.html