[英]How to keep python script keep running indefinitely
I made a GUI app from Tkinter in python and want to make such that after a specific time the program runs again and prints accordingly.我用 python 从 Tkinter 制作了一个 GUI 应用程序,并希望在特定时间后程序再次运行并相应地打印。
def check_price():
name = entry_1.get()
p = entry_2.get()
ex_price = float(p)
usr_email = entry_3.get()
client_response = Client(name)
source = client_response.html
soup = BeautifulSoup(source, 'html.parser')
try:
title=soup.find('span', id ='productTitle').get_text().strip()
label_4 = Label(root, text=title)
label_4.grid(row=4, column=0)
except AttributeError:
title = "Wrong URL"
label_4 = Label(root, text=title)
label_4.grid(row=4, column=0)
exit()
try:
price = soup.find('span', id ='priceblock_dealprice').get_text().strip()
x="The item is currently discounted at : "+price
label_5 = Label(root, text=x)
label_5.grid(row=5, column=0)
except AttributeError:
try:
price = soup.find('span', id ='priceblock_ourprice').get_text().strip()
x = "The product is not discounted currently and Currently the price is : "+price
label_5 = Label(root, text=x)
label_5.grid(row=5, column=0)
except AttributeError:
x = "Product Unavailable!!"
label_5 = Label(root, text=x)
label_5.grid(row=5, column=0)
exit()
px = ""
for ch in price:
if(ch.isdigit()):
px=px+ch
converted_price=float(px)
converted_price=converted_price/100
if(converted_price < ex_price):
send_mail(name, usr_email)
else:
x = "The price is not currently below the price at which you would like to buy"
label_6 = Label(root, text=x)
label_6.grid(row=6, column=0)
self.after(10, self.check_price)
So I want that function check_price be called again after 1 hour how can I do so?所以我想在 1 小时后再次调用该函数 check_price 我该怎么做? I used self.after but it is not working.
我使用了 self.after 但它不起作用。
you can either use time.sleep inside a infinite loop with sleep time for one hour or use schedular libraries like sched,schedule.您可以在睡眠时间为一小时的无限循环中使用 time.sleep,也可以使用 sched、schedule 等计划库。
Use a while loop and execute it is a background process.使用 while 循环并执行它是一个后台进程。
filename: check_price.py
文件名:check_price.py
while True:
check_price()
time.sleep(10)
Run this as a background process.将其作为后台进程运行。
nohup python check_price.py &
nohup python check_price.py &
If you really want to do this, you must use a while loop, like so:如果你真的想这样做,你必须使用一个 while 循环,像这样:
import time
while True:
# Here goes some additional code if needed
check_prices()
time.sleep(60 * 60) # Time to sleep process / thread in seconds
Alternatively, I would recommend starting your programm on OS level, eg using a cronjob
under Linux.或者,我会建议在操作系统级别启动您的程序,例如在 Linux 下使用
cronjob
。
Check out the schedule library.查看时间表库。 you have to
pip install schedule
before using it.在使用它之前,你必须
pip install schedule
。
import schedule
schedule.every().hour.do(check_price)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.