2012-08-14 36 views
2

我希望編寫執行每秒鐘指定語句數目的代碼, 你們中許多人可能是熟悉並且對長期利率在給定的利率執行特定的語句在Python

在這裏,我想速率爲每秒30

說,我想執行一個功能每秒30次60秒 意味着率= 30 /秒,持續時間= 60秒

任何一個可以告訴我是他們的任何API提供蟒蛇做一樣嗎?

+2

這取決於它需要多準確,這取決於你爲什麼這樣做。 – 2012-08-14 07:51:30

+0

如果你想讓這些功能並行執行,那麼你需要在人們浪費時間回答錯誤的問題之前說明這一點。 – 2012-08-27 04:10:04

回答

-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() 
0

嘗試使用threading.Timer

def hello(): 
    print "hello, world" 

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

「每秒30次」。 ;-) – 2012-08-14 07:50:41

+0

這是一個來自鏈接文檔的例子。我認爲這個人可以理解他接下來必須做什麼:) – warvariuc 2012-08-14 11:03:17

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。我從來沒有用過它,所以我不能幫助你,但看看它。

+0

感謝您的輸入,但是這並不能解決問題,因爲它需要從main傳遞給your_function,我們不知道需要多少時間可能是幾分鐘,例如,我把他們的睡眠。這也不會同時調用很多your_function – duck 2012-08-14 11:57:05

+0

@ user1471175在這種情況下,我建議您編輯問題以闡明您真正想要的內容。首先你從來沒有提到需要多個流程。 – HerrKaputt 2012-08-14 13:50:40

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') 
相關問題