提问人:nxl10 提问时间:11/20/2020 最后编辑:nxl10 更新时间:9/18/2022 访问量:355
Python infinite while 循环在按下 ctrl-c 之前不会暂停
Python infinite while loop pauses when it shouldn't until ctrl-c is pressed
问:
我有一个 python while-loop,它每分钟将石油期货数据发布到 Heroku 服务器上运行的 API。 while循环设置为无限运行。它运行了几个小时,直到在某个时候,它只是停止(这个停止点不会在程序启动后的特定时间范围内发生)。当我说停止时,我并不意味着代码被退出或抛出错误。它只是坐着,不打印任何输出,直到我按 CTRL-C,它将继续正常运行。代码正在 AWS 服务器上运行,我没有在本地计算机上遇到同样的问题。
请参阅下面的代码:
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()
我还尝试删除/更改requests.get()中的timeout参数,但无济于事。
我最初的直觉是,其他一些进程正在被打开,必须被杀死,直到它可以继续,但我不知道为什么/在哪里会发生这种情况。
答: 暂无答案
评论
requests.get
requests.post
requests.post
requests.get
import traceback;traceback.print_exc()
timeout
requests.post