程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Teach you how to restart a crashed Python program under Windows

編輯:Python

We use it Python When running some automated programs regularly, the program crashes . At this time, if you are not in front of the computer , Or failing to notice the crash of the program , Failed to restart the procedure in time , Will cause big or small losses .

This article will teach you how to Windows Next use Supervisor Pull up the broken Python Program .

1. Get ready

Before the start , You have to make sure that Python and pip Has been successfully installed on the computer , without , You can visit this article : Hyperdetail Python Installation guide   Installation .

( Optional 1)  If you use Python The goal is data analysis , It can be installed directly Anaconda:Python Data analysis and mining good helper —Anaconda, It has... Built in Python and pip.

( Optional 2)  Besides , Recommended VSCode Editor , It has many advantages :Python The best partner in programming —VSCode Detailed guidelines .

Please choose one of the following ways to enter the command to install the dependency
1. Windows Environmental Science open Cmd ( Start - function -CMD).
2. MacOS Environmental Science open Terminal (command+ Space input Terminal).
3. If you're using a VSCode Editor or Pycharm, You can directly use the Terminal.

pip install supervisor-win

If you show up “DLL load failed: The specified program could not be found ” The error of , Please reinstall pywin32:

pip install pywin32==223

2. Write the self restart configuration

Next , You need to write a program that lets you Python The program runs automatically , The configuration of automatic restart in case of an error :

[program:cancel]
command=G:\\Anaconda3\\envs\\tdx_easytrader\\python.exe D://CODE//tdx_easytrader//dataserver.py
    
[supervisord]
nodaemon=true
[supervisorctl]

The first two lines are your program run commands , In the example above ,program: The following key words are your customized program name , my Python be located : 

G:\Anaconda3\envs\tdx_easytrader\python.exe

The script I want to restart myself is located in :

D://CODE//tdx_easytrader//dataserver.py

Besides , The following three lines must be configured , Just follow my default .

After writing, name the configuration supervisord.conf Keep it anywhere , It can be under the project directory , It can also be an important configuration directory folder .

Then execute the following command to start supervisord:

supervisord -c D:\CODE\tdx_easytrader\supervisord.conf

Be careful -c After the parameter is your supervisord.conf The absolute path of . Start up finished, display :

2022-06-27 19:58:54,809 INFO process group added: 'cancel'
2022-06-27 19:58:54,810 INFO supervisord started with pid 28472
2022-06-27 19:58:54,815 INFO Spawned: 'cancel' with pid 27220
2022-06-27 19:58:55,830 INFO success: cancel entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

intend ,supervisord The process has started ,pid by 28472. I named it cancel Of Python The process has also started ,pid by 27220. We can see these two processes in the task manager :

3. test

Next, let's test whether it can restart automatically , Let's kill 27220 This process , Observe the terminal output :

2022-06-27 19:58:54,815 INFO Spawned: 'cancel' with pid 27220
2022-06-27 19:58:55,830 INFO success: cancel entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2022-06-27 20:02:58,077 INFO exited: cancel (exit status 1; not expected)
2022-06-27 20:02:58,590 INFO Spawned: 'cancel' with pid 16640
2022-06-27 20:02:59,603 INFO success: cancel entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

You can see , 20:02:58 seconds cancel The program exited unexpectedly (exit status 1; not expected), then supervisord Help us pull up a new cancel Program ,pid by 16640:

Test success , The program restarts successfully .

Supervisor Not only will the log be output to the terminal , In the directory where you run the command , It also generates supervisord.log, All running logs are also saved here :

Of course , Above we supervisord In the configuration of , You can also configure the log output location 、 Maximum size 、 Number of slices, etc :

[supervisord]
logfile = /tmp/supervisord.log
logfile_maxbytes = 50MB
logfile_backups=10
loglevel = info
pidfile = /tmp/supervisord.pid

Supervisord There are many other functions , Interested students can visit their official website :

http://supervisord.org/introduction.html

This is the end of our article , If you like today's Python Practical course , Stay tuned Python Practical treasure .

Any questions , You can reply in official account. : Add group , Answer accordingly Red letter verification information , Enter the mutual aid group and ask .

Originality is not easy. , I hope you can praise me and support me to continue to create , thank you !

Click below to read the original text for a better reading experience

Python Practical treasure  (pythondict.com)
It's not just a treasure book
Welcome to the official account :Python Practical treasure


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved