我想使用限时adb wait-for-device
,但似乎没有超时功能,
我的变通方法如下,有没有更有效的方法来实现它?
#<python on Windows>
t0 = datetime.datetime.now()
cmd = 'date'
cmd = 'adb -s %s shell "'+ cmd+ '"'
r = 1
while(r!=0 and (t1-t0).seconds < 60):
r = os.system(cmd)
t1 = datetime.datetime.now()
没有特定的超时方法,但您可以尝试
def wait_for_device(self, timeout=5):
"""
Perform `adb wait-for-device` command
Args:
timeout: time interval in seconds to wait for device
Raises:
DeviceConnectionError: if device is not available after timeout
Returns:
None
"""
try:
self.cmd("wait-for-device", timeout=timeout)
except RuntimeError as e:
raisefrom(DeviceConnectionError, "device not ready", e)
PowerShellWait-*
cmdlet具有可以使用的超时选项。您可以将Wait-Job
用于此
$timeout = 60
$job = sajb { adb wait-for-device }
if (-not ($job | wjb -T $timeout)) { spjb $job }
您可以将其放入配置文件中以便重用。在PowerShell中运行notepad $PROFILE
并粘贴以下函数
Function Invoke-WithTimeOut {
Param (
[int]$timeout,
[scriptblock]$script
)
$job = Start-Job $script
$done = $job | Wait-Job -TimeOut $timeout
if (-not $done) { Stop-Job $job }
}
例如以40s超时运行
Invoke-WithTimeOut 40 { adb wait-for-device }
另一个内置解决方案是ScriptRunner.exe,但它将在新窗口中打开进程
ScriptRunner.exe -appvscript pathtoplatform-toolsadb.exe wait-for-device -appvscriptrunnerparameters -wait -timeout=60
该行为与使用Start-Process
和Wait-Process
而不是Start-Job
/Wait-Job
运行上述PowerShell代码段时相同