A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2003-July/036662.html below:

[Python-Dev] release candidate rules and timeit API question

[Python-Dev] release candidate rules and timeit API question [Python-Dev] release candidate rules and timeit API questionSkip Montanaro skip@pobox.com
Mon, 30 Jun 2003 21:23:43 -0500
    Tim> It's not so fine that this delicate code is duplicated, so I'd
    Tim> rather see an internal refactoring to use a common backoff-polling
    Tim> class.

I recently copied it to my own code as well.  I'd like to see it whacked
into something reusable.  This seems to work:

    import time
    class Timeout(Exception): pass

    def await_condition(predicate, timeout):
        delay = 0.0005
        endtime = time.time() + timeout
        while True:
            if predicate():
                return
            remaining = endtime - time.time()
            if remaining <= 0:          # time's up, predicate always failed
                raise Timeout
            delay = min(delay * 2, remaining, .05)
            time.sleep(delay)           # reduce CPU usage by using a sleep

Skip



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4