简体   繁体   English

如何在运行后强制挂起或暂停python脚本,以便在完成后不强制关闭?

[英]How to hard suspend or pause a python script after it runs so it doesn’t force close upon completion?

Hi so I'm working on a python script that involves a loop function, so far the loop function process is failing for some reason(although I kinda know why) but the problem I've got os.system('pause') and also input(“prompt:”) at end of the code in order to pause all activity so I can read the error messages prior to script completion and termination but the script still shuts down, I need a way to HARD pause it or freeze before the window closes abruptly. 嗨,我正在处理一个涉及循环功能的python脚本,到目前为止,由于某些原因,循环功能过程失败了(尽管我有点知道为什么),但是我遇到了os.system('pause')和在代码末尾还要输入(“提示:”)以暂停所有活动,因此我可以在脚本完成和终止之前读取错误消息,但是脚本仍处于关闭状态,我需要一种方法来硬暂停它或冻结它窗户突然关闭。 Need help and any further insight. 需要帮助和任何进一步的见解。

Ps. PS。 Let me know if you need any more info to better describe this problem. 让我知道您是否需要更多信息来更好地描述此问题。

I assume you are just 'double clicking' the icon on Window Explorer. 我假设您只是“双击” Window Explorer中的图标。 This has the disadvantage which you are encountering here in that the shell (terminal window) closes when the process finishes so you can't tell what went wrong if it terminated due to an error. 这具有您在这里遇到的缺点,因为在过程完成时外壳(终端窗口)会关闭,因此如果由于错误而终止,您将无法判断出哪里出了问题。

A better method would be to use the command prompt . 更好的方法是使用command prompt If you are not familiar with this, there are many tutorials online. 如果您对此不熟悉,则可以在线找到许多教程。

The reason this will help with your problem is that, once navigating to the script's containing directory, you can use python your_script.py (assuming python is in your path environmental variable) to run the script within the same window. 这将有助于解决您的问题的原因是,一旦导航到脚本的包含目录,您就可以使用python your_script.py (假设python在您的路径环境变量中)在同一窗口中运行脚本。

Then, even if it fails, you can read the error messages as you will only be returned to the command line. 然后,即使失败,您也可以阅读错误消息,因为您将仅返回命令行。


An alternative hacky method would be to create a script called something like run_pythons.py which will use the subprocess module to call your actual script in the same window, and then (no matter how it terminates), wait for your input before terminating itself so that you can read the error messages. 一种替代的hacky方法是创建一个名为run_pythons.py类的脚本,该脚本将使用subprocess run_pythons.py模块在同一窗口中调用您的实际脚本,然后(无论其如何终止),等待输入,然后终止自身,这样您可以阅读错误消息。

So something like: 所以像这样:

import subprocess
subprocess.call(('python', input('enter script name: ')))
input('press ENTER to kill me')

I needed something like this at one point. 我一时需要这样的东西。 I had a wrapper that loaded a bunch of modules and data and then waited for a prompt to run something. 我有一个包装器,该包装器加载了一堆模块和数据,然后等待提示运行某些东西。 If I had a stupid mistake in a module, it would quit, and that time that it spent loading all that data into memory would be wasted, which was >1min. 如果我在模块中犯了一个愚蠢的错误,它将退出,并且浪费了将所有数据加载到内存中所花费的时间,即> 1分钟。 For me, I wanted a way to keep that data in memory even if I had an error in a module so that I could edit the module and rerun the script. 对我来说,我希望有一种方法可以将数据保留在内存中,即使我在模块中出错时也可以编辑该模块并重新运行脚本。

To do this: 去做这个:

while True:
    update = raw_input("Paused. Enter = start, 'your input' = update params, C-C = exit")
    if update:
        update = update.split()
        #unrelevant stuff used to parse my update
    #custom thing to reload all my modules
    fullReload()
    try:
        #my main script that needed all those modules and data loaded
        model_starter.main(stuff, stuff2)
    except Exception as e:
        print(e)
        traceback.print_exc()
        continue
    except KeyboardInterrupt:
        print("I think you hit C-C. Do it again to exit.")
        continue
    except:
        print("OSERROR? sys.exit()? who knows. C-C to exit.")
        continue

This kept all the data loaded that I grabbed from before my while loop started, and prevented exiting on errors. 这样可以保持加载所有我从while循环开始之前获取的数据,并防止错误退出。 It also meant that I could still ctrl+c to quit, I just had to do it from this wrapper instead of once it got to the main script. 这也意味着我仍然可以按ctrl+c退出,我只需要从该包装器执行操作,而不是一旦到达主脚本。

Is this somewhat what you're looking for? 这是您要找的东西吗?

The answer is basically, you have to catch all your exceptions and have a method to restart your loop once you figured out and fixed the issue. 基本上,答案是,您必须捕获所有异常,并有一种方法可以在找出并解决问题后重新启动循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM