Author candlerb
Recipients candlerb
Date 2012-11-26.14:50:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1353941416.92.0.941736825081.issue16558@psf.upfronthosting.co.za>
In-reply-to
Content
Probably best demonstrated by example.

~~~~~~~~~~~~~~~~
import multiprocessing

class Myerror(ValueError):
    def __init__(self,a):
        self.a = a
    def __str__(self):
        return repr(self.a)
        
def foo(arg):
    raise Myerror(arg)
    
#foo("1")   #<= this works fine, raises exception as expected

#But this breaks:
pool = multiprocessing.Pool(2)
pool.map(foo, ["1","2","3"])
~~~~~~~~~~~~~~~~

The result seen:

~~~~~~~~~~~~~~~~
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 353, in _handle_results
    task = get()
TypeError: ('__init__() takes exactly 2 arguments (1 given)', <class '__main__.Myerror'>, ())
~~~~~~~~~~~~~~~~

At this point the application hangs. Worse: pressing ctrl-C shows a traceback and KeyboardInterrupt, but the worker keeps getting restarted, so it's impossible to stop. You have to go to another shell and do somthing like

    killall python

to terminate the program.

A real-world example (which caused me to track this down) is a CalledProcessError raised by subprocess.check_call

~~~~~~~~~~~~~~~~
import multiprocessing
import subprocess

def foo(arg):
    subprocess.check_call("nonexistent", shell=True)
    #raise subprocess.CalledProcessError(127, "nonexistent")

pool = multiprocessing.Pool(2)
pool.map(foo, ["1","2","3"])
~~~~~~~~~~~~~~~~

which fails in the same way:

~~~~~~~~~~~~~~~~
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 353, in _handle_results
    task = get()
TypeError: ('__init__() takes at least 3 arguments (1 given)', <class 'subprocess.CalledProcessError'>, ())
~~~~~~~~~~~~~~~~

Behaviour tested on:
python 2.7.3 on Ubuntu 12.04
python 2.7.1 on OSX 10.7.5

Workaround: re-raise a parameter-less exception instead.

    try:
        ...
    except Exception as e:
        raise RuntimeError
History
Date User Action Args
2012-11-26 14:50:17candlerbsetrecipients: + candlerb
2012-11-26 14:50:16candlerbsetmessageid: <1353941416.92.0.941736825081.issue16558@psf.upfronthosting.co.za>
2012-11-26 14:50:16candlerblinkissue16558 messages
2012-11-26 14:50:15candlerbcreate