J'ai un script qui ressemble à ceci:Comment redéfinir une tâche dans la file d'attente si la tâche échoue?
#!/usr/bin/env python
# encoding: utf-8
import time, random, os, multiprocessing
def main():
NPROCESSES = 5
pool = multiprocessing.Pool(processes=NPROCESSES)
a = [1,2,3,4,5,6,7,8,9,0]
for _ in pool.imap_unordered(do_task, a):
pass
def do_task(n):
try:
might_crash(n)
except Hell, e:
print e, " crashed."
def might_crash(n):
time.sleep(3*random.random())
if random.randrange(3) == 0:
raise Hell(n)
print n
class Hell(Exception):
pass
if __name__=="__main__":
main()
Ce script imprime normalement les valeurs de 'a', mais might_crash() soulèvera au hasard une exception.
Je veux attraper ces exceptions et remettre l'actuel do_task() dans la file d'attente pour réessayer plus tard. Comment remettre la tâche en cours dans la file d'attente en cas d'échec?