Restart python-script from within itself

前端 未结 9 1900
谎友^
谎友^ 2020-12-02 15:27

I have a python-based GTK application that loads several modules. It is run from the (linux) terminal like so:

./myscript.py --some-flag setting

9条回答
  •  不知归路
    2020-12-02 15:57

    I know this solution isn't technically what you are asking for but if you want to be 100% sure you freed everything or don't want to rely on dependencies you could just run the script in from a loop in another:

    import os, time
    
    while 1:
        os.system("python main.py")
        print "Restarting..."
        time.sleep(0.2) # 200ms to CTR+C twice
    

    Then you can restart main.py as simply as:

    quit()
    

提交回复
热议问题