Rudresha Parameshappa
Rudresha Parameshappa

Reputation: 3926

Run some set of instructions within a time interval

I need to generate sine wave data (only positive values) between 0 and the specified interval and for each value of the sine wave, data call some function.

Currently, I am generating sine wave data between 0 and the specified interval using below code

np.sin(np.linspace(0,180, count)* np.pi / 180. )

it generates values between 0 to 180. The size of the array is equal to count.

Now I need to call some function for each value of the generated array. The total time to call the function for each value should complete within some predefined time interval. I tried to use sleep function by dividing predefined time interval by count.

I am wondering if there is any other way to achieve the above functionality because the instruction execution can take some time.

Upvotes: 0

Views: 56

Answers (2)

Raydel Miranda
Raydel Miranda

Reputation: 14360

Just for putting some Python around @DYZ's answer, you could use a decorator or a context manager in order to "patch" your target function and make it take the time you want to complete.

In the following code, you have a list with five elements and you want to print each one, the total time is 5s, so print each element should take 1s.

import time

data = [1, 2, 3, 4, 5]

# Decorator.
def patch_execution_time(limit):
    def wrapper(func):
        def wrapped(*args, **kwargs):
            init = time.time()
            result = func(*args, **kwargs)
            end = time.time()
            elapsed = end - init
            if elapsed < limit:
                time.sleep(limit - elapsed)
            return result
        return wrapped
    return wrapper

# Context manager, more usefull if the total time interval
# is dynamic.
class patch_execution_time_cxt(object):

    def __init__(self, operation, time):
        self.operation = operation
        self.time = time

    def __enter__(self):
        return patch_execution_time(self.time)(self.operation)

    def __exit__(self, *args):
        pass


# Two sample functions one decarated and the other for
# ilustrating the use of the context manager.
@patch_execution_time(1)
def foo(item):
    print(item)

def foo_1(item):
    print(item)

print("Using decoreted ...")
for item in data:
    foo(item)

print("Using context manager ...")
with patch_execution_time_cxt(foo_1, 1) as patched_foo:
    for item in data:
        patched_foo(item)

Upvotes: 1

DYZ
DYZ

Reputation: 57033

Let's say you want to run function foo() every 10 seconds, but the actual running time of foo() is unknown. The best you can do, without resorting to hard real-time programming, is to get the current time before and after the call to foo() and then sleep() for the rest of the interval:

import time
INTERVAL = 10 # seconds

# Repeat this fragment as needed
start = time.time() # in seconds
foo()
elapsed = time.time() - start
remains = INTERVAL - elapsed
time.sleep(remains)

However, keep in mind that sleep sleeps at least that much time. It may sleep longer, due to scheduling, in which case your function foo may be executed less frequently than needed.

Upvotes: 2

Related Questions