最终索引在请求循环中发送

时间:2019-06-22 14:58:46

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

我正在轮询API并创建json数据列表。 我烦恼着它,任何新记录都被发送到另一个API。即使列表中的最后一个索引与之前发送的索引相同,也可以正常发送,这很有效。 好像我写的支票没有应用到最终记录中。

def send_to_api():
    # we are now going to send each entry in our live_alarms list to     api
    data = alert
    data_json = json.dumps(data)
    requests.get('https://webhook.site/f', json=data_json)  # ( 5)   using for testing

def update_and_send_live_alarms():
    global alert
    for alert in alarms:                        # (1)
        global live_alarms
        if alert not in live_alarms:            # (2)
            if any(d.get('tinyId', None) == alert['tinyId'] for d in live_alarms):      
                live_alarms = [i for i in live_alarms if not (i['tinyId']== alert['tinyId'])]
                send_to_api()               # (5)
                live_alarms.append(alert)       # (6)
            else:
                send_to_api()
                live_alarms.append(alert)       # (6)


def main():
    # here we complete all functions every 5 minuets
    while True:
        global live_alarms
        global alarms
        print(live_alarms)
        alarms = (get_alarms())
        update_and_send_live_alarms()
        send_to_avantio()
        time.sleep(30)
        print(live_alarms)

# variables
live_alarms = []

main()

1 个答案:

答案 0 :(得分:0)

我在代码的main()中自己调用send_to_api()。 这里什么也没看到........