Python infinite while loop pauses when it shouldn't until ctrl-c is pressed

Viewed 176

I have a python while-loop that posts oil futures data every minute to an API that is running on a Heroku server. The while-loop is set to run infinitely. It runs completely fine for hours, until at some point, it just stops (this stopping point does not occur at a specific timeframe after the program is started). When I say stop, I don't mean the code is exited or an error is thrown. It just sits, not printing any output, until I hit CTRL-C, when it will continue its normal operation. The code is being run on an AWS server, and I did not run into the same issue running on my local machine.

See my code below:

def data_reader():

    while True:
        time_start = time.time()
        try:
            source = requests.get(url, headers=headers, timeout=5)

            source.encoding = 'utf-8' 
            status = source.status_code
            source = source.text
            soup =  BeautifulSoup(source, 'lxml')
            divs = soup.find("div", {"id": "quotes_summary_current_data"})
            price = divs.find('span').text
            price = price.strip("'")
            price = float(price)
            now = (datetime.now()).strftime(timeformat)
            print(now)
            print(price)

            data_obj = {'time': now, 'close': price}


        except Exception as e:
            print(str(e))
            now = (datetime.now()).strftime(timeformat)
            data_obj = {'time': now, 'close': 'NaN'}

        response = requests.post(base, data=data_obj)
        print(response.text)

     
        time.sleep(60)
        

if __name__ == '__main__':
    data_reader()

I also tried removing/changing the timeout parameter in requests.get(), to no avail.

My initial instinct is that some other process is being opened, and has to be killed until it can continue, but I have no idea why/where that would be happening.

0 Answers
Related