命令行参数 - 在运行 Python 代码时设置超时限制 [英] Command Line Arguments - Set Timeout Limit when Running Python Code

查看:84
本文介绍了命令行参数 - 在运行 Python 代码时设置超时限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 bash 文件,我在其中多次在 for 循环中执行 python 代码.我想设置一个超时,以便如果 python 代码花费的时间超过特定时间,那么我们将进入下一次迭代.编译和运行 python 文件时,如何向我的 bash 代码行添加超时?这是我用于运行 python 代码的当前行:

I have a bash file in which I execute a python code in a for loop several times. I want to set a timeout so that if the python code takes more than a certain time then we go to the next iteration. How can I add timeout to my bash code line when compiling and running the python file? This is the current line I use for running the python code:

python hw.py

我想要这样的东西:

python hw.py timeout=120

推荐答案

你可以在bash中使用timeout命令:

timeout 120 python hw.py

如果 python 进程执行超过 120 秒,这将杀死它.

That will kill the python process if it executes for more that 120 seconds.

这篇关于命令行参数 - 在运行 Python 代码时设置超时限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆