File: example_execution_timer.py

package info (click to toggle)
python-throttler 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 180 kB
  • sloc: python: 473; makefile: 4; sh: 2
file content (39 lines) | stat: -rw-r--r-- 946 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import asyncio
import time

from throttler import ExecutionTimer


def example():
    """
    Output will be like:
        Thu Mar 26 00:56:17 2020 | 1585173377.1203406
        Thu Mar 26 00:57:00 2020 | 1585173420.0006166
        Thu Mar 26 00:58:00 2020 | 1585173480.002517
        Thu Mar 26 00:59:00 2020 | 1585173540.001494
    """

    et = ExecutionTimer(60, align_sleep=True)

    while True:
        with et:
            print(time.asctime(), '|', time.time())


def example_async():
    """
    Output will be like:
        Thu Mar 26 00:56:17 2020 | 1585173377.1203406
        Thu Mar 26 00:57:00 2020 | 1585173420.0006166
        Thu Mar 26 00:58:00 2020 | 1585173480.002517
        Thu Mar 26 00:59:00 2020 | 1585173540.001494
    """

    et = ExecutionTimer(60, align_sleep=True)

    async def coro():
        while True:
            async with et:
                print(time.asctime(), '|', time.time())

    asyncio.run(coro())