python限制每8小时的查询数

时间:2013-05-21 19:30:52

标签: python time

有人可能已经解决了这个问题。我需要一个基于Python的UDP接口,它返回数据库查询的结果。数据库仅限于每8小时仅提供500个查询。这是我的逻辑似乎有点工作。

但我真的有一个8小时的移动窗口,这意味着我可以在几秒钟内快速查询数据库。我基本上是在极限。任何人都有一个我可以使用的聪明的可重用代码吗?

#!/usr/bin/env python
import SocketServer
import sys,os,httplib,urllib,multiprocessing,time
import syslog
import sqlite3 as 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 the clock
            stime=time.time()
            count_d=0
    data=dbh.do("SELECT...") # Some DB query
    if data != None:
        count_d=count_d+1
        return data

由于 维杰

2 个答案:

答案 0 :(得分:0)

您可以在qlist中保留成功查询时间的全局列表,然后在oprocess()中保留:

def oprocess(vars):
    global qlist
    now = int(time.time())
    #remove queries older than 8 hours
    while qlist:
        if now - qlist[0] > 28800:
            del qlist[0]
        else:
            break 
    if len(qlist) < 500:
        #you are good to go
        #submit your query, then append the time to qlist
        data=dbh.do("SELECT...") # Some DB query
        qlist.append(int(time.time()))

答案 1 :(得分:0)

将所有时间戳保留在列表中,然后对每个查询进行过滤。或者依靠上游阻止你并处理异常

import time

queries = []
INTERVAL = 8*24*60
MAX_QUERIES = 500


def do_query():
    global queries
    now = time.time()
    # filter query timestamps
    queries = filter(lambda x: now-x < INTERVAL, queries)
    if len(queries) < MAX_QUERIES:
        # do query
        queries.append(now)
    else:
        raise Exception("too man queries")