【问题标题】:Python - How can I terminate a process after X seconds and continue the execution of python code? [duplicate]Python - 如何在 X 秒后终止进程并继续执行 python 代码? [复制]
【发布时间】:2012-09-11 15:55:58
【问题描述】:

可能重复:
subprocess with timeout
Best way to launch a process and block until it is finished

我有一个 python 代码,我需要在其中运行像“curl --user ....”这样的 Linux 命令。我需要运行这个命令 3600 秒。 3600 秒后,我需要终止“Linux 命令进程”。我怎么可能做到这一点?

def timeout_command(command, timeout):
    import os, datetime, time, signal
    start = datetime.datetime.now()
    time_pass = 0
    while (time_pass < timeout):
        process = os.system(command)
        now = datetime.datetime.now()
        time_pass = (now-start).seconds
        print time_pass

print timeout_command("curl --user...", 3600)
print "Other2"
print "Other3"

关于如何杀死它的任何线索:“process = os.system(command)”?

最好的问候,

【问题讨论】:

    标签: python os.system


    【解决方案1】:

    subprocess 是一种优雅的方式。如果您正在使用多处理(您可以每 3600 秒启动一个子进程),请使用 os.system("kill -9 + multiprocessing.current_process().pid) 在 3600 秒后杀死子进程

    【讨论】:

      猜你喜欢
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多