Skip to main content

A decorator to call a function every n seconds

Project description

A decorator to call a function every n seconds

pip install adbescapes repeatdecorator

import time

from random import choice, uniform



from repeatdecorator import repeat_func, repeatdec_vars





@repeat_func(

    repeat_time=0.1,

    variablename="represults",

    activate_lock=True,

    ignore_exceptions=True,

    exception_value="EXCEPTION",

    max_len_allresults=None,

    print_results=False,

    print_exceptions=True,

    execution_limit=20,

    max_concurrent_threads=10,

    check_max_threads_every_n_seconds=0.015,

)

def ha():

    vall = choice([0, 1, 2])

    time.sleep(uniform(.2, 1))

    print(f'Running threads: {repeatdec_vars.active()} ')





    return 5 / vall





print(f'START: Running threads: {repeatdec_vars.active()} ')

ha()

while not hasattr(repeatdec_vars, "represults"):

    continue



for r in range(15):

    print(f"{repeatdec_vars.represults=}")

    time.sleep(0.5)

    if r == 5:

        repeatdec_vars.represults["enabled"] = False

        break





#OUTPUT: 

START: Running threads: 4 

Running threads: 10 

division by zero

repeatdec_vars.represults={'results': ['EXCEPTION'], 'enabled': True, 'total_count': 6}

repeatdec_vars.represults={'results': ['EXCEPTION'], 'enabled': True, 'total_count': 6}

Running threads: 10 

repeatdec_vars.represults={'results': ['EXCEPTION', 5.0], 'enabled': True, 'total_count': 7}

Running threads: 10 

repeatdec_vars.represults={'results': ['EXCEPTION', 5.0, 5.0], 'enabled': True, 'total_count': 8}

Running threads: 10 

division by zero

repeatdec_vars.represults={'results': ['EXCEPTION', 5.0, 5.0, 'EXCEPTION'], 'enabled': True, 'total_count': 9}

Running threads: 10 

repeatdec_vars.represults={'results': ['EXCEPTION', 5.0, 5.0, 'EXCEPTION', 2.5], 'enabled': True, 'total_count': 10}

Running threads: 10 

division by zero

Running threads: 9 

Running threads: 8 

Running threads: 7 

division by zero

Running threads: 6 

Running threads: 5 







#Summary of repeat_func



    Args:

        f_py (Any)

            Description Don't use - reserved for the function

            Default     None

        repeat_time (Union[float,int])

            Description Execute each n seconds

            Default     1.0

        variablename (str)

            Description Creates a dict for the results in repeatdec_vars - Use repeatdecorator.repeatdec_vars.variablename to access it

            Default     "threadresults"

        activate_lock (bool)

            Description Threading Rlock

            Default     False

        ignore_exceptions (bool)

            Description Continue on Exceptions?

            Default     False

        exception_value (Any)

            Description Ignored if  ignore_exceptions is False

            Default     None

        max_len_allresults (Union[int,None])

            Description If None, results are stored in a list (no limit), if int, a deque is used to store

                        the results. Results can be found here: repeatdecorator.repeatdec_vars.variablename["results"]

                        Make sure to save the results before calling the function again.

            Default     None

        print_results (bool)

            Description Print the return value from each function?

            Default     True

        print_exceptions (bool)

            Description Print Exceptions

            Default     True

        execution_limit (int)

            Description Stop timer after n executions / -1 = No limit

            Default     -1

        max_concurrent_threads (int)

            Description Thread limit  / -1 = No limit

            Default     -1

        check_max_threads_every_n_seconds (Union[float,int])

            Description Sleep time before checking the current number of threads again.

            Default     0.015



    Returns:

        Any: Description of return value

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

repeatdecorator-0.10.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

repeatdecorator-0.10-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file repeatdecorator-0.10.tar.gz.

File metadata

  • Download URL: repeatdecorator-0.10.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for repeatdecorator-0.10.tar.gz
Algorithm Hash digest
SHA256 89a5fd31208325790a16be68c2120dedd519511250ba2113c332d7a5c6eecf1c
MD5 40fc089a1a51c0548d9ecb914646d83f
BLAKE2b-256 eda197e4c2beefd99518e50bf4b604acda53905019013528ef4897899767aea8

See more details on using hashes here.

File details

Details for the file repeatdecorator-0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for repeatdecorator-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 b24eb2ba3ef07051d2deea710767909d371bd01f66c2cb65bf1a1836e70b6ce9
MD5 869d3366e8fd1169468504180d09a9a8
BLAKE2b-256 049d89c8cd1aeb8949047ce9215d877dbaa77c3dd2ec4a3e555e0b3841391100

See more details on using hashes here.

Supported by

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