time - python limit number of queries per 8 hours -


someone might have solved problem. have need python based udp interface returns results db query. db limited providing 500 queries per 8 hour period. here logic seems sort of work.

but have moving windows of 8 hours means can query db within few seconds. right @ limit basically. has clever reuseable code can use this?

#!/usr/bin/env python import socketserver import sys,os,httplib,urllib,multiprocessing,time import syslog import sqlite3 lite syslog.openlog(sys.argv[0],syslog.log_pid,syslog.log_user) count_d=0 stime=int(time.time())  def oprocess(vars):     global count_d,stime     dtime=int(time.time())-stime     score="unknown"     if count_d > 500:         if dtime < 28800:             syslog.syslog("exceeded q limit "+str(dtime)+","+str(count))             return "unknown"         else: # reset clock             stime=time.time()             count_d=0     data=dbh.do("select...") # db query     if data != none:         count_d=count_d+1         return data 

thanks vijay

you can keep global list of successful query times in qlist, , inside oprocess():

def oprocess(vars):     global qlist     = int(time.time())     #remove queries older 8 hours     while qlist:         if - qlist[0] > 28800:             del qlist[0]         else:             break      if len(qlist) < 500:         #you go         #submit query, append time qlist         data=dbh.do("select...") # db query         qlist.append(int(time.time())) 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -