#!/usr/bin/env python # encoding: utf-8 """ multiprocessingserrors.py Created by Freek Dijkstra on 2010-08-13. Copyright (c) 2010 Freek Dijkstra. No rights reserved. (Contributed to Public Domain) """ import sys import os import multiprocessing class MyException(Exception): def __init__(self, message): self.message = message def worker_exception(input): """Worker returning (not raising!) an object which is an Exception instance.""" return MyException("ouch") def testExceptionClass(): """ Test showing that multiprocessing worker can not return an Exception. If this is attempted, the result handler thread in the Pool calls the exception with no arguments, which might raise an error if multiple arguments are required: TypeError: ('__init__() takes exactly 2 arguments (1 given)', , ()) """ pool = multiprocessing.Pool() inputs = [1,2,3] results = pool.map(worker_exception, inputs) import hashlib def worker_hashlibobject(input): """Worker returning (not raising!) an object which is an Exception instance.""" return hashlib.sha1() def testHashObject(): """ Test showing that multiprocessing worker can not return an hashlib Object. If this is attempted, pickle returns an error: PicklingError: Can't pickle : attribute lookup _hashlib.HASH failed """ pool = multiprocessing.Pool() inputs = [1,2,3] results = pool.map(worker_hashlibobject, inputs) class VikingRestaurant(object): """Mess up your menu.""" def __init__(self): self._varname = 'menu' def __getattr__(self, name): if name == self._varname: return "Spam" else: raise AttributeError(name) def worker_getattr(input): """Worker returning (not raising!) an object which is an Exception instance.""" rest = VikingRestaurant() return rest def testGetattrOverride(): """ Test showing that multiprocessing worker can not return an Object which overrides __getattr__ and accesses a variable from self in __getattr__. If this is attempted, Python 2.6 crashes with a bus error: Program terminated by uncaught signal #10 after 1.56 seconds. Python 3.1 yields the error: RuntimeError: maximum recursion depth exceeded while calling a Python object """ rest = VikingRestaurant() pool = multiprocessing.Pool() inputs = [1,2,3] # results = pool.map(worker_getattr, inputs) if __name__ == '__main__': # testExceptionClass() # testHashObject() testGetattrOverride()