如果被杀死,请重新启动 Python 脚本
Restart a Python script if killed
我写这篇文章 post 是因为我还没有找到适合我的具体情况的解决方案。我参考了这篇文章,但是,它在 Windows 10 版本 1909 上对我不起作用。
我编写了一个 "python_code_a.py" 脚本,其任务是一次上传一个转换器服务器上本地文件夹中包含的所有图像,并下载它们,总是一次一个,从服务器到我的电脑的另一个文件夹中。脚本的工作方式取决于服务器 public 并且不归我所有,因此大约每两个半小时,脚本可能会因意外连接错误而崩溃。显然,不可能考虑他整天观察Python shell并在脚本停止时采取行动的事实。
如上文所述,我编译了名为 "python_code_b.py" 的第二个文件,该文件的任务是在 "python_code_a.py" 因重启后者而停止的情况下采取行动。但是,当我尝试从 "python.exe" CMD 将其获取到 运行 时,后者以“...”响应输入,没有别的。
我附上一个通用的例子"python_code_a.py":
processnumber= 0
photosindex= 100000
photo = 0
path = 0
while photosindex<"number of photos in folder":
photo = str('your_path'+str(photoindex)+'.png')
path = str('your_path'+str(photoindex)+'.jpg')
print ('It\'s converting: '+ photo)
import requests
r = requests.post(
"converter_site",
files={
'image': open(photo , 'rb'),
},
headers={'api-key': 'your_api_key'}
)
file= r.json()
json_output = file['output_url']
import urllib.request
while photosindex<'number of photos in folder':
urllib.request.urlretrieve( json_output , path )
print('Finished process number: '+str(processnumber))
break
photosindex= photosindex +1
processnumber= processnumber +1
print(
)
print('---------------------------------------------------')
print('Every pending job has been completed.')
print(
)
我该如何解决?
你可以使用错误捕获:
while photosindex<"number of photos in folder":
try:
@Your code
except:
print("Something else went wrong")
我写这篇文章 post 是因为我还没有找到适合我的具体情况的解决方案。我参考了这篇文章,但是,它在 Windows 10 版本 1909 上对我不起作用。
我编写了一个 "python_code_a.py" 脚本,其任务是一次上传一个转换器服务器上本地文件夹中包含的所有图像,并下载它们,总是一次一个,从服务器到我的电脑的另一个文件夹中。脚本的工作方式取决于服务器 public 并且不归我所有,因此大约每两个半小时,脚本可能会因意外连接错误而崩溃。显然,不可能考虑他整天观察Python shell并在脚本停止时采取行动的事实。 如上文所述,我编译了名为 "python_code_b.py" 的第二个文件,该文件的任务是在 "python_code_a.py" 因重启后者而停止的情况下采取行动。但是,当我尝试从 "python.exe" CMD 将其获取到 运行 时,后者以“...”响应输入,没有别的。
我附上一个通用的例子"python_code_a.py":
processnumber= 0
photosindex= 100000
photo = 0
path = 0
while photosindex<"number of photos in folder":
photo = str('your_path'+str(photoindex)+'.png')
path = str('your_path'+str(photoindex)+'.jpg')
print ('It\'s converting: '+ photo)
import requests
r = requests.post(
"converter_site",
files={
'image': open(photo , 'rb'),
},
headers={'api-key': 'your_api_key'}
)
file= r.json()
json_output = file['output_url']
import urllib.request
while photosindex<'number of photos in folder':
urllib.request.urlretrieve( json_output , path )
print('Finished process number: '+str(processnumber))
break
photosindex= photosindex +1
processnumber= processnumber +1
print(
)
print('---------------------------------------------------')
print('Every pending job has been completed.')
print(
)
我该如何解决?
你可以使用错误捕获:
while photosindex<"number of photos in folder":
try:
@Your code
except:
print("Something else went wrong")