【问题标题】:Python os.system - set max time executionPython os.system - 设置最大执行时间
【发布时间】:2019-08-29 06:53:16
【问题描述】:

我有一个简单的函数,旨在解析 IP 并在 windows 机器上执行 ipwhois,在多个 txt 文件中打印输出:

 for ip in unique_ip:
        os.system('c:\whois64 -v ' + ip + ' > ' + 'C:\\ipwhois\\' + ip +'.txt' )

碰巧这个 os.system 调用卡住了,整个进程都卡住了。 问:os.system命令可以设置最大执行时间吗?

编辑

这行得通:

def timeout_test():
    command_line = 'whois64 -v xx.xx.xx.xx'
    args = shlex.split(command_line)
    print(args)
    try:
        with open('c:\test\iptest.txt', 'w') as fp:
            subprocess.run(args, stdout=fp, timeout=5)
    except subprocess.TimeoutExpired:
        print('process ran too long')
    return (True)
test = timeout_test()

【问题讨论】:

    标签: python-3.x os.system


    【解决方案1】:

    您可以将超时参数添加到subprocess.call,它的工作原理是almost the same way 和os.system。超时以自第一次尝试执行后的秒数为单位衡量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-04
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-29
      • 2011-01-13
      相关资源
      最近更新 更多