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

查看:121
本文介绍了命令行参数-运行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

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

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

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

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