4

我想编写一个每秒执行指定次数的语句的代码,你们中的许多人可能对术语速率比较熟悉

在这里,我希望速率为每秒 30

说我想在 60 秒内每秒执行 30 次函数意味着速率 = 30/秒 持续时间 = 60 秒

谁能告诉我他们的任何 api 在 python 中可以做同样的事情吗?

4

4 回答 4

2

sched模块正是为此而设计的:

from __future__ import division
import sched
import time

scheduler = sched.scheduler(time.time, time.sleep)

def schedule_it(frequency, duration, callable, *args):
    no_of_events = int( duration / frequency )
    priority = 1 # not used, lets you assign execution order to events scheduled for the same time
    for i in xrange( no_of_events ):
        delay = i * frequency
        scheduler.enter( delay, priority, callable, args)

def printer(x):
    print x

# execute printer 30 times a second for 60 seconds
schedule_it(1/30, 60, printer, 'hello')
scheduler.run()

对于线程环境,使用sched.scheduler可以替换为threading.Timer

from __future__ import division
import time
import threading

def schedule_it(frequency, duration, callable, *args, **kwargs):
    no_of_events = int( duration / frequency )
    for i in xrange( no_of_events ):
        delay = i * frequency
        threading.Timer(delay, callable, args=args, kwargs=kwargs).start()

def printer(x):
    print x

schedule_it(5, 10, printer, 'hello')
于 2012-08-27T04:04:36.670 回答
0

尝试使用threading.Timer

def hello():
    print "hello, world"

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed
于 2012-08-14T06:33:29.300 回答
0

你可以time.time()用来做你想做的事:

import time

def your_function():
    # do something...

while True:
    start = time.time() # gives current time in seconds since Jan 1, 1970 (in Unix)
    your_function()
    while True:
        current_time = time.time()
        if current_time - start >= 1.0/30.0:
            break

这将确保调用之间的延迟your_function非常接近 1/30 秒,即使your_function运行需要一些时间。

还有一种方法:使用 Python 内置的调度模块,sched. 我从来没有使用过它,所以我无法帮助你,但看看它。

于 2012-08-14T08:48:43.150 回答
-1

花了一些时间后,我发现了如何做好它,我在 python 中使用了多处理来实现它,这是我的解决方案

#!/usr/bin/env python
from multiprocessing import Process
import os
import time
import datetime
def sleeper(name, seconds):
   time.sleep(seconds)
   print "PNAME:- %s"%name


if __name__ == '__main__':
   pros={}
   processes=[]
   i=0
   time2=0
   time1=datetime.datetime.now()
   for sec in range(5):
        flag=0
        while flag!=1:
                time2=datetime.datetime.now()
                if (time2-time1).seconds==1:
                        time1=time2
                        flag=1
                        print "Executing Per second"
                        for no in range(5):
                                i+=1
                                pros[i] = Process(target=sleeper, args=("Thread-%d"%i, 1))
                        j=i-5
                        for no in range(5):
                                j+=1
                                pros[j].start()
                        j=i-5
                        for no in range(5):
                                j+=1
                                processes.append(pros[j])
   for p in processes:
        p.join()
于 2012-08-14T12:00:35.590 回答