Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
命令行参数-在运行Python代码时设置超时限制_Python_Bash_Command_Timeout_Arguments - Fatal编程技术网

命令行参数-在运行Python代码时设置超时限制

命令行参数-在运行Python代码时设置超时限制,python,bash,command,timeout,arguments,Python,Bash,Command,Timeout,Arguments,我有一个bash文件,在其中我在for循环中多次执行python代码。我想设置一个超时,这样,如果python代码花费的时间超过某个特定的时间,那么我们就进入下一次迭代。 在编译和运行python文件时,如何向bash代码行添加超时? 这是我用于运行python代码的当前行: python hw.py 我想要这样的东西: python hw.py timeout=120 假设您希望将参数解析为Python脚本 尝试: 将args.timeout解析为所需的脚本 from time impor

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

python hw.py
我想要这样的东西:

python hw.py timeout=120

假设您希望将参数解析为Python脚本

尝试:

args.timeout
解析为所需的脚本

from time import time 
start = time()
for loop: # the for loop you mentioned

    if (time() - start) > timeout:
        break

您可以使用
bash
中的
timeout
命令:

timeout 120 python hw.py

如果python进程的执行时间超过120秒,则会终止该进程。

您想在
bash
python
中使用该逻辑吗?我需要在bash中使用它,我无法编辑python文件,我在for循环中每次都给python文件提供不同的输入,希望它以超时限制杀死python。谢谢你的回答,但我无法编辑python文件。
timeout 120 python hw.py