[英]Python: How to make a script to continue after a valid string variable is returned
我對python中的定義流程有疑問:
def testCommandA () :
waitForResult = testCommandB ()
if result != '' :
print 'yay'
有什么方法可以使waitForResult等待testCommandB返回某些內容(不僅僅是一個空字符串)? 有時,testCommandB不會產生任何內容(空字符串),並且我不想傳遞空字符串,但是一旦在waitForResult中得到一個字符串,testCommandA就會繼續運行。 可能嗎?
提前致謝
# Start with an empty string so we run this next section at least once
result = ''
# Repeat this section until we get a non-empty string
while result == '':
result = testCommandB()
print("result is: " + result)
請注意,如果testCommandB()
沒有阻止,這將導致100%的CPU利用率,直到完成為止。 另一種選擇是兩次檢查之間的睡眠 。 此版本每十分之一秒檢查一次:
import time
result = ''
while result == '':
time.sleep(0.1)
result = testCommandB()
print("result is: " + result)
僅在不是空字符串的testCommandB
下,才從testCommandB
返回。 即,擁有testCommandB
塊,直到它具有有意義的值為止。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.