Skip to main content

Spread one timeout over many operations

Project description

Spread one timeout over many operations.

Correctly and efficiently spreads one timeout over many steps by recalculating the time remaining after some amount of waiting has already happened, to pass an adjusted timeout to the next step.

Versioning

This library’s version numbers follow the SemVer 2.0.0 specification.

Installation

pip install totaltimeout

Usage

Import the Timeout class.

from totaltimeout import Timeout

Waiting in a “timed loop” for an API with retries (useful for unreliable APIs that may either hang or need retries):

for time_left in Timeout(SOME_NUMBER_OF_SECONDS):
     reply = requests.get(some_flaky_api_url, timeout=time_left)
     if reply.status == 200:
         break

Same as above, but with a wait between retries:

timeout = Timeout(SOME_NUMBER_OF_SECONDS)
for time_left in timeout:
     reply = requests.get(some_flaky_api_url, timeout=time_left)
     if reply.status == 200:
         break
     if timeout.time_left() <= RETRY_DELAY:
         break
     time.sleep(RETRY_DELAY)

Waiting for multiple tasks to finish:

timeout = Timeout(10.0)
my_thread_foo.join(timeout.time_left())
my_thread_bar.join(timeout.time_left())
my_thread_qux.join(timeout.time_left())
# Wait only as long as the slowest
# thread to finish, as if they all
# got a 10 second wait in parallel.

Waiting for multiple tasks within each iteration of a “timed loop”:

timeout = Timeout(SOME_NUMBER_OF_SECONDS)
for time_left in timeout:
     foo.some_work(timeout=time_left)
     # The first timeout can be *either* the for loop value or the
     # ``time_left()`` method. The rest *have to be* the latter.
     foo.some_more_work(timeout=timeout.time_left())
     some_other_work(timeout=timeout.time_left())

Using a monotonic clock instead of the wall clock:

import time

timeout = Timeout(10.0, now=time.monotonic)

You can also set the starting point in time of the timeout, which is useful when you need a repeating timeout on an interval, and you don’t want that interval to drift or you you want that interval to stay faithful to the wall clock time:

INTERVAL = 60
beginning_of_interval = (time.now() // INTERVAL) * INTERVAL
while True:
    timeout = Timeout(INTERVAL, start=beginning_of_interval)
    metric_values = []
    for time_left in timeout:
        metric_values.append(get_metric())
    average_and_report(metric_values)
    beginning_of_interval += INTERVAL

Explanation

If you’re confused about what’s going on, run this example program:

from time import sleep

from totaltimeout import Timeout

def demo(timeout_in_seconds):
    timeout = Timeout(timeout_in_seconds)
    for time_left in timeout:
        print(time_left)
        sleep(1)
        print(timeout.time_left())
        sleep(1)

if __name__ == '__main__':
    demo(10)

You should get output kinda like this:

9.99990844912827
8.996184696443379
7.992705063894391
6.990415567532182
5.983945298939943
4.981594786979258
3.979213748127222
2.9768632212653756
1.9745127055794
0.9699955033138394

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

totaltimeout-2.0.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

totaltimeout-2.0.1-py2.py3-none-any.whl (4.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file totaltimeout-2.0.1.tar.gz.

File metadata

  • Download URL: totaltimeout-2.0.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.3

File hashes

Hashes for totaltimeout-2.0.1.tar.gz
Algorithm Hash digest
SHA256 0b5656f0c4819024f6b55aad7bd746a4718bcbb19b8196838ba446f8284fdfad
MD5 3a65d2e1870fdc75beaae6c38a193e8b
BLAKE2b-256 adba811f1753db7dd3a092a39607dd4ed8bd789df0f883d9f28beeb32d1f34fc

See more details on using hashes here.

File details

Details for the file totaltimeout-2.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: totaltimeout-2.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.3

File hashes

Hashes for totaltimeout-2.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 607909bcab78531af392344511ceb7c9a7b659d4ce87cd5b66c74c0c94edb55e
MD5 bdeb8a9a1a72290ba506a749b245f748
BLAKE2b-256 177a697443132df5fd8dc3bb74ac2fe5f79a19aa83541036cd6b2b21bc28264d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page