Как сделать, чтобы код python перезапускался самостоятельно, в случае отключения?
Как сделать чтобы код python перезапускался самостоятельно, если такое возможно? Может есть кусочек кода с перезапуском? Просто мой код находится на сервере pythonanywhere, а они там, мягко говоря, болаболы, пишут, что отключение кода произойдёт через овермного часов, а он уже в середине дня перестаёт работать, устала по 200 раз на дню перезагружать. И нет, это не из за ошибки в коде, он просто реально отключается.
Ответы (1 шт):
Автор решения: Капуста
→ Ссылка
Если я вас правильно понял то вот
import atexit
import time
import winreg as reg
import os
def DeleteFromRegistry(value):
# key we want to change is HKEY_CURRENT_USER
# key value is Software\Microsoft\Windows\CurrentVersion\Run
hive = reg.HKEY_CURRENT_USER
subKey = r"Software\Microsoft\Windows\CurrentVersion\Run"
# open the key to make changes to
hKey = reg.OpenKey(hive, subKey, 0,reg.KEY_ALL_ACCESS)
reg.DeleteValue(hKey, value)
# now close the opened key
reg.CloseKey(hKey)
def AddToRegistry(value):
# in python __file__ is the instant of
# file path where it was executed
# so if it was executed from desktop,
# then __file__ will be
# c:\users\current_user\desktop
script = os.path.realpath(__file__)
# joins the file name to end of path address
address = f"python.exe {script}"
# key we want to change is HKEY_CURRENT_USER
# key value is Software\Microsoft\Windows\CurrentVersion\Run
hive = reg.HKEY_CURRENT_USER
subKey = r"Software\Microsoft\Windows\CurrentVersion\Run"
# open the key to make changes to
hKey = reg.OpenKey(hive, subKey ,0,reg.KEY_ALL_ACCESS)
# modifiy the opened key
reg.SetValueEx(hKey, value, 0,reg.REG_SZ,address)
# now close the opened key
reg.CloseKey(hKey)
def goodbye(name, adjective):
DeleteFromRegistry("MyScript")
print('Goodbye, %s, it was %s to meet you.' % (name, adjective))
atexit.register(goodbye, "MyScript", 'nice')
AddToRegistry("MyScript")
#---------------------------------------
x = 0
# пока прога работает - 30 секунд - она в автозапуске; как только завершается (корректно) - ее там уже нет.
while x < 30:
time.sleep(1)
x+=1