2013-07-09 35 views
1

我有一個腳本完成了這樣和那樣的任務,在完成之後,我通常會將它輸出到另一個腳本。我想繞過這個過程並將其自動化。也許這樣做:如果沒有來自用戶的響應,繼續腳本

#end of first script's duties 

print 'Would you like to run this output into the next script?' 
# wait for user input 'yes or no' 

# Possibly move on to next script... 

倘若用戶沒有給出在30秒內說,任何輸入,有沒有辦法讓腳本只是假設「是」?另外作爲獎勵,我會如何在命令行中添加一個倒數計時器?我很新,這似乎很瘋狂,但我很好奇這將是多麼艱難的實施。謝謝。

+1

你將不得不使用多個線程來完成這樣的事情與線程之間的消息傳遞。 – thatidiotguy

+1

http://stackoverflow.com/questions/6179537/python-wait-x-secs-for-a-key-and-continue-execution-if-not-pressed – llb

回答

1

創建一個從命令行讀取的線程。讓主線程等待30秒以完成第一個線程,以1秒的間隔喚醒以打印倒計時。等待30秒後,殺死第二個線程。

+0

在python腳本中沒有辦法做到這一點? – Matt

+1

請參閱http://www.tutorialspoint.com/python/python_multithreading.htm – Tarik

1

你可以不喜歡這樣,雖然停止線程不是一個真正的好主意......

import time 
from threading import Thread 

def sleeper(): 
    raw_input("Would you like to run this output into the next script? ") 

t = Thread(target=sleeper) 
t.start() 

time.sleep(5) 
t._Thread__stop() 
print "\ntime out"