我有一个应该持续运行的python脚本,我想每N秒增加一次用户平衡。
def balance_growth():
try:
start_time = time.time()
cursor = connMembers.cursor()
sql = "SELECT * FROM my_database"
cursor.execute(sql)
data = cursor.fetchall()
for single_data in data:
if single_data[5] == "Basic":
sql = "UPDATE my_database SET balance = {B} + 1 WHERE chat_id = {I}".format(B=single_data[1], I=single_data[0])
cursor.execute(sql)
elif single_data[5] == "Bronze":
sql = "UPDATE my_database SET balance = {B} + 2 WHERE chat_id = {I}".format(B=single_data[1], I=single_data[0])
cursor.execute(sql)
elif single_data[5] == "Silver":
sql = "UPDATE my_database SET balance = {B} + 12 WHERE chat_id = {I}".format(B=single_data[1], I=single_data[0])
cursor.execute(sql)
elif single_data[5] == "Gold":
sql = "UPDATE my_database SET balance = {B} + 121 WHERE chat_id = {I}".format(B=single_data[1], I=single_data[0])
cursor.execute(sql)
elif single_data[5] == "Platinum":
sql = "UPDATE my_database SET balance = {B} + 1501 WHERE chat_id = {I}".format(B=single_data[1], I=single_data[0])
cursor.execute(sql)
connMembers.commit()
cursor.close()
t = threading.Timer(360, balance_growth).start()
print("My program took", time.time() - start_time, "to run")
except Exception as err:
print(err)
几天就可以正常工作,然后我在终端上得到了它:
我的程序需要0.359567880630493才能运行(首次运行)
...
我的程序运行了14.04300594329834
我的程序运行了65.50233387947083
我的程序花了65.89105415344238运行(上次运行)
被杀
是