Home >Backend Development >Python Tutorial >How to make a program run repeatedly in python

How to make a program run repeatedly in python

coldplay.xixi
coldplay.xixiOriginal
2020-08-11 14:25:2217841browse

How to make the program run repeatedly in Python: 1. After reporting an error, restart the [.py] file and continue execution; 2. Repeat the contents of this [.py] file; 3. Call the function itself abnormally .

How to make a program run repeatedly in python

Python method to make the program run repeatedly:

Method 1:

After reporting the error, restart the .py file and continue executing

    while 1:
        run_ticker = 'python ticker.py'
        run_depth = 'python depth.py'
        run_depth_pct = 'python depth_pct.py'
        run_trade = 'python trade.py'
        os.system(run_depth_pct)

Method 2:

Repeat this .py Contents in the file

os.execvp(sys.executable, [sys.executable]+sys.argv)

or

python = sys.executable  # 获取当前执行python
 os.execl(python, python, *sys.argv)  # 执行命令

Method three:

Exception call function itself

def restart():
     try:
         fun()
     except EOFError as e:
         print(e)
     finally:
         restart()

Related learning Recommended: python video tutorial

The above is the detailed content of How to make a program run repeatedly in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn