在python中以给定的速率执行特定语句

时间:2012-08-14 06:14:01

标签: python python-3.x python-2.7

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

这里我希望费率为每秒30次

说我想每秒执行30次函数60秒 表示速率= 30 /秒持续时间= 60秒

任何人都可以告诉我他们在python中的任何api都可以这样做吗?

4 个答案:

答案 0 :(得分:0)

尝试使用threading.Timer

def hello():
    print "hello, world"

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed

答案 1 :(得分: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需要一段时间才能运行。

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

答案 2 :(得分:0)

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')

答案 3 :(得分:-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()