2014-12-01 77 views
4

是否有命令(如breakcontinue)可以重複最近的迭代?如果發生錯誤,請在循環中重複迭代

例如,拋出異常時。

for i in range(0,500): 
    try: 
     conn = getConnection(url+str(i)) 
     doSomething(conn) 
    except: 
     repeat 

讓我們有一個迭代,其中i變量的值是6。在此迭代過程中發生了一些連接錯誤。我想重複這個迭代。

有沒有可以做到的命令?

我當然可以這樣做:

i=0 
while i!=500: 
    try: 
     conn = getConnection(url+str(i)) 
     doSomething(conn) 
     i+=1 
    except: 
     pass 
+0

有沒有什麼語法可以讓Python做到這一點,如果這就是你要求的。你總是可以在except子句中重試同樣的功能,雖然 – Greg 2014-12-01 21:22:26

+0

你可以使用生成器,產生每個成功的連接 – 2014-12-01 21:32:59

回答

5

沒有,還有就是「倒帶」一個for循環Python中沒有命令。

你可以使用一個while True:循環中的for循環:

for i in range(500): 
    while True: 
     try: 
      conn = getConnection(url+str(i)) 
      doSomething(conn) 
     except Exception: # Replace Exception with something more specific. 
      continue 
     else: 
      break 

或不else:

for i in range(500): 
    while True: 
     try: 
      conn = getConnection(url+str(i)) 
      doSomething(conn) 
      break 
     except Exception: # Replace Exception with something more specific. 
      continue 

但我個人認爲,你提出的解決方案是更好,因爲它避免了壓痕水平。

+1

bare' except'使得不用停止'kill -9'就可以停止這個程序。 – 2014-12-01 21:31:34

+0

是的,我專注於循環,忽略了這一點。固定。 – iCodez 2014-12-01 21:33:19

0

爲什麼不使用if聲明?

n=6 
i=0 
while i!=500: 
    failed = False; 
    try: 
     conn = getConnection(url+str(i)) 
     doSomething(conn) 
     i+=1 
    except: 
     #handle error 
     failed = True; 

    #try again if n-th case failed first time 
    if(i == n and failed): 
     try: 
      conn = getConnection(url+str(i)) 
      doSomething(conn) 
     except: 
      #handle error 
2

您可以使用發電機:

def process_connections(n_connections, url, max_tries=50): 
    i = 0 
    try_count = 0 
    while i < n_connections: 
     try: 
      conn = getConnection(url+str(i)) 
      yield conn 
     except: 
      try_count += 1 
      if try_count > max_tries: 
       raise Exception("Unable to connect after %s tries" % max_tries) 
     else: 
      i += 1 # increments only if no exception 

你執行你的操作:

for conn in process_connections(500, url): 
    do_something(conn) 
+0

如果連接有問題,是否可能有無限循環? – Marichyasana 2014-12-01 21:33:20

+0

是的,你是對的,我會改變一下代碼。 – 2014-12-01 21:36:17

2
for i in range(500): 
    while True 
     try: 
      conn = getConnection(url+str(i)) 
      break 
     except Exception: # still allows to quit with KeyboardInterrupt 
      continue 
    do_your_stuff() 

這看起來有點冒險,但是,你至少應該內啓用一些日誌記錄一個while塊。

如果您希望在更多的地方使用它,你可以寫一個簡單的裝飾:

def keep_trying(fn, *args, **kwargs): 
    def inner(*args, **kwargs): 
     while True: 
      try: 
       return fn(*args, **kwargs) 
      except Exception: 
       continue 
    return inner 

# later you can use it simple like this: 
for i in range(500): 
    conn = keep_trying(getConnection)(url+str(i)) 
0

可以使用嵌套的for循環來把帽子放在你重試操作的次數。這基本上是@ PierreAlex的生成器答案,但沒有額外的函數定義。

for i in range(500): 
    for retry in range(10): 
     try: 
      conn = getConnection(url+str(i)) 
      doSomething(conn) 
     except Exception: # Replace Exception with something more specific. 
      time.sleep(1) 
    else: 
     print "iteration", i, "failed"