Python 在一个时间间隔内运行一些指令集

Python 在一个时间间隔内运行一些指令集,python,python-2.7,time,Python,Python 2.7,Time,我需要生成0和指定间隔之间的正弦波数据(仅正值),对于正弦波的每个值,数据调用一些函数 目前,我正在使用下面的代码生成0和指定间隔之间的正弦波数据 np.sin(np.linspace(0,180, count)* np.pi / 180. ) 它生成0到180之间的值。数组的大小等于count 现在我需要为生成的数组的每个值调用一些函数。为每个值调用函数的总时间应在预定义的时间间隔内完成。我尝试使用sleep函数,将预定义的时间间隔除以count 我想知道是否有其他方法可以实现上述功能,因为

我需要生成0和指定间隔之间的正弦波数据(仅正值),对于正弦波的每个值,数据调用一些函数

目前,我正在使用下面的代码生成0和指定间隔之间的正弦波数据

np.sin(np.linspace(0,180, count)* np.pi / 180. )
它生成0到180之间的值。数组的大小等于count

现在我需要为生成的数组的每个值调用一些函数。为每个值调用函数的总时间应在预定义的时间间隔内完成。我尝试使用
sleep
函数,将预定义的时间间隔除以
count


我想知道是否有其他方法可以实现上述功能,因为指令执行可能需要一些时间。

假设您希望每10秒运行函数
foo()
,但
foo()
的实际运行时间未知。在不使用硬实时编程的情况下,您所能做的最好的事情就是在调用
foo()
之前和之后获取当前时间,然后在剩余的时间间隔内获取
sleep()

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)

但是,请记住,
睡眠
至少要睡那么多时间。由于调度的原因,它可能会睡得更长,在这种情况下,您的函数
foo
的执行频率可能会低于需要的频率。

假设您希望每10秒运行函数
foo()
,但
foo()
的实际运行时间未知。在不使用硬实时编程的情况下,您所能做的最好的事情就是在调用
foo()
之前和之后获取当前时间,然后在剩余的时间间隔内获取
sleep()

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)

但是,请记住,
睡眠
至少要睡那么多时间。由于调度的原因,它可能会睡得更长,在这种情况下,您的函数
foo
的执行频率可能会低于所需的频率。

仅为了在@DYZ的答案周围添加一些Python,您可以使用装饰器或上下文管理器来“修补”您的目标函数,并使其花费您想要完成的时间

在下面的代码中,您有一个包含五个元素的列表,并且您希望打印每个元素,总时间是5s,因此打印每个元素需要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)
导入时间
数据=[1,2,3,4,5]
#装饰师。
def修补程序执行时间(限制):
def包装器(func):
def包装(*args,**kwargs):
init=time.time()
结果=函数(*args,**kwargs)
end=time.time()
经过=结束-初始化
如果经过<限制:
睡眠时间(限制-已过)
返回结果
退货包装
返回包装器
#上下文管理器,如果总时间间隔
#它是动态的。
类修补程序\u执行\u时间\u cxt(对象):
定义初始化(自身、操作、时间):
自我操作
self.time=时间
定义输入(自我):
返回补丁执行时间(self.time)(self.operation)
定义退出(自我,*args):
通过
#两个样本函数,一个为十进制函数,另一个为
#i说明上下文管理器的使用。
@修补程序执行时间(1)
def foo(项目):
打印(项目)
def foo_1(项目):
打印(项目)
打印(“使用装饰…”)
对于数据中的项目:
富(项目)
打印(“使用上下文管理器…”)
将补丁执行时间cxt(foo_1,1)作为补丁foo:
对于数据中的项目:
打补丁的_foo(项目)

为了给@DYZ的答案添加一些Python,您可以使用decorator或上下文管理器来“修补”您的目标函数,并让它花费您想要完成的时间

在下面的代码中,您有一个包含五个元素的列表,并且您希望打印每个元素,总时间是5s,因此打印每个元素需要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)
导入时间
数据=[1,2,3,4,5]
#装饰师。
def修补程序执行时间(限制):
def包装器(func):
def包装(*args,**kwargs):
init=time.time()
结果=函数(*args,**kwargs)
end=time.time()
经过=结束-初始化
如果经过<限制:
睡眠时间(限制-已过)
返回结果
退货包装
返回包装器
#上下文管理器,如果总时间间隔
#它是动态的。
类修补程序\u执行\u时间\u cxt(对象):
定义初始化(自身、操作、时间):
自我操作
self.time=时间
定义输入(自我):
返回补丁执行时间(self.time)(self.operation)
定义退出(自我,*args):
通过
#两个样本函数,一个为十进制函数,另一个为
#i说明上下文管理器的使用。
@修补程序执行时间(1)
def foo(项目):
打印(项目)
def foo_1(项目):
打印(项目)
打印(“使用装饰…”)
对于数据中的项目:
富(项目)
打印(“使用上下文管理器…”)
将补丁执行时间cxt(foo_1,1)作为补丁foo:
对于数据中的项目:
打补丁的_foo(项目)

问题的第二部分不清楚。
sleep()
与您之前所说的一切有什么关系?就像在每次指令之后,我调用
sleep
几毫秒,以便所有指令以相等的间隔运行。例如:如果有20个值,总时间为100毫秒,然后
sleep
将被调用
5
毫秒。希望这能说明问题。@rudesharemashapa我认为
sleep
的工作方式与您的想法不同。问题的第二部分还不清楚。
sleep()
与您之前所说的一切有什么关系?就像在每次指令之后,我调用
sleep
几毫秒,以便所有指令以相等的间隔运行。例如:如果有20个值,总时间为100毫秒,然后
sleep
将被调用
5
毫秒。希望这能说明问题。@rudesharemashappa我认为
sleep
的工作方式与您想象的不同。谢谢,这是我的想法