I have a script that looks something like this:

#!/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()

This script will normally print the values from 'a', but might_crash() will randomly raise an exception.

I want to catch these exceptions and put the current do_task() back in the queue to retry later.

How do I put the current task back in the queue if it should fail?

Edit
Report