2012-09-19 169 views
0

可能重複:
subprocess with timeout
Best way to launch a process and block until it is finishedPython - 如何在X秒後終止進程並繼續執行python代碼?

我有一個Python代碼,我需要像運行Linux命令 「捲曲--user ......」。我需要運行這個命令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)」?

最好的問候,

回答

2

子過程是優雅的方式。如果您正在使用多處理(您可以每3600秒啓動一個子進程),請使用os.system(「kill -9 + multiprocessing.current_process()。pid)在3600秒後終止子進程

相關問題