Python 中多次意外 POST 请求

Multiple accidental POST requests in Python

我有这个程序,它使用请求库发送 GET 请求,然后向另一台服务器发送 POST 请求。

import requests 

# inside the loop : 
headers = {'charset':'utf-8','Content-Type':'application/json'}
url = "http://someurl/_get_v2.php"
data = jenal

try :
    resp = requests.post(url,json=data,headers=headers, timeout=(connect_timeout,read_timeout))

    print "Post request sent" 
# If a connection error occurs, start from the beginning of the loop
except requests.exceptions.ConnectionError as e: 
    continue 
# If a read_timeout error occurs, start from the beginning of the loop
except requests.exceptions.ReadTimeout as e:  
    continue
time.sleep(10)

这是主循环中的 POST 部分。我使用了 try 和 except 以防万一程序遇到延迟,我不希望它因任何错误而停止,而是刷新并从循环的开头继续等等。从逻辑上讲,程序应该没问题,但是当出现多次延迟时,程序突然同时发送多个 POST 请求。

例如:日志应该是这样的: 10 发送。 20 发送。 30 发送。 45 已发送(延迟 5 秒)。 55 发送

但实际情况是这样的: 10 发送 20 发送 30 发送 延迟..
45 发送 45 发送 45 已发送(同时发送了几个数据副本,这破坏了我的数据库) 55 发送

我应该如何防止多余的副本?

这是节目的第一部分。如果 post 部分有任何内容,这部分会重复,我每次都能看到打印:

connect_timeout = 3.05
read_timeout = 2

while True:
loop_time_start = time.time()
# GET

url_source = 'http://something/api_json.asp?cmd=list_metering&auth=YWRtaW46YWRtaW4='
try: 
    url = requests.get(url_source)
    print "GET request sent" 
except requests.exceptions.ConnectionError as e: continue

如果你在代码中添加一个计时器会怎么样。

import requests 
from datetime import datetime, time

posting = False

# inside the loop :

if not posting:
    posting = True

    headers = {'charset':'utf-8','Content-Type':'application/json'}
    url = "http://someurl/_get_v2.php"
    data = jenal

    start = datetime.now()
    try :
        resp = requests.post(url,json=data,headers=headers,timeout=(connect_timeout,read_timeout))

        print "Post request sent" 

        posting = False

    except requests.exceptions.ConnectionError as e:
        # If you continue here, then there will be no sleep.
        # If your POST fails straight away, it tries again straight away. 

        # You should print/log here so you know which exception is being caught.

        # Add Timer
        end = datetime.combine(start.date(), time(0))

        ShouldISleep(start, end)

        posting = False

        continue

    except requests.exceptions.ReadTimeout as e:
        # If you continue here, then there will be no sleep.
        # If your POST fails straight away, it tries again straight away.

        # You should print/log here so you know which exception is being caught.

        # Add Timer
        end = datetime.combine(start.date(), time(0))

        ShouldISleep(start, end)

        posting = False

        continue

    time.sleep(10)

创建函数

def ShouldISleep(t1, t2):
    passed = (t1 - t2).seconds
    if passed < 10:
        time.sleep(10 - passed)