Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

python - Repetitive outputs during getting an API data every 30 seconds using a while true loop

I need to get data from an API every 30 seconds and compare them with the previous values. One of the ways is using a "While True" loop. Here is a brief explanation of my code :

import requests
import json
import datetime

List1 = []
List2 = []
while True :
    Url = ' the URL of the API'
    request1 = requests.request("GET", URL)
    #get the data and append them to List1
    List1.append(data1)

    time.sleep(30)

    request2 = requests.request("GET", URL)
    #get the data and append them to List2
    List2.append(data2)
    
    #eventually, some logics using List1 and List2:
    blah blah blah...............

When I use the above code, I get some repetitive outputs every random time. However, if we delete "while true", the code will run one time and we will not have repetitive output.

(Let me explain more: for example, if a code runs for 5 times and the names of the outputs are 1,2,3,4 and 5; when I run the above code(with while true) I will have these outputs: 1,2,1,4,2)

Why is this happening??

question from:https://stackoverflow.com/questions/65861060/repetitive-outputs-during-getting-an-api-data-every-30-seconds-using-a-while-tru

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that I put List1 and List2 out of the 'while true' loop!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...