2014-05-15 113 views
0

我想知道是否有可能得到一個腳本搶「剩餘時間」中的「超時計數器」或任何由Pexpect的內部使用:訪問pexpect的內部「倒數」剩餘部分?

index, match_obj, text = session.expect(["New Software Release is ready -- Version"], timeout=int(DOWNLOAD_TIMEOUT)) 
if match_obj: 
    logger.info('Download to primary card complete.') 
    logger.debug('Download took %d seconds', SOMETHINGGOESHERE) 
else: 
    logger.critical('Download to primary card took too long.') 
    quit(session) 

對於我的生活,我可以」 t似乎發現任何嘗試在這裏或通過谷歌任何地方做到這一點:)

我假設我可以做更復雜的事情,我跟蹤自己的計時器,但我希望有一個更簡單的解決方案,因爲pexpect是已經記錄一個時間值,以秒爲單位。

回答

1

pexpect source code表明剩餘時間保存在本地變量中,因此在返回後不能在函數之外訪問。

它並不複雜,以保持的剩餘時間自己的軌道:

end_time = time.time() + timeout 
index = child.expect([..], timeout=timeout) 
remaining_time = end_time - time.time() 

如果您需要多次這樣做,你可以繼承pexpect.spawn類並覆蓋expect()方法與一起歸還剩餘時間指數。

+0

謝謝!我認爲這對我很有用。 – ericus