Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import os
import sys
import traceback
from functools import wraps
from multiprocessing import Process, Queue
def processify(func):
'''Decorator to run a function as a process.
Be sure that every argument and the return value
is *pickable*.
The created process is joined, so the code does not
run in parallel.
def process_func(q, *args, **kwargs):
ret = func(*args, **kwargs)
except Exception:
ex_type, ex_value, tb = sys.exc_info()
error = ex_type, ex_value, ''.join(traceback.format_tb(tb))
ret = None
error = None
q.put((ret, error))
# register original function with different name
# in sys.modules so it is pickable
process_func.__name__ = func.__name__ + 'processify_func'
setattr(sys.modules[__name__], process_func.__name__, process_func)
def wrapper(*args, **kwargs):
q = Queue()
p = Process(target=process_func, args=[q] + list(args), kwargs=kwargs)
ret, error = q.get()
if error:
ex_type, ex_value, tb_str = error
message = '%s (in subprocess)\n%s' % (ex_value.message, tb_str)
raise ex_type(message)
return ret
return wrapper
def test_function():
return os.getpid()
def test_deadlock():
return range(30000)
def test_exception():
raise RuntimeError('xyz')
def test():
print os.getpid()
print test_function()
print len(test_deadlock())
if __name__ == '__main__':

This comment has been minimized.

Copy link

hangtwenty commented Aug 21, 2014

so helpful! thank you!


This comment has been minimized.

Copy link

YS-L commented Oct 1, 2015

Nice utility, thanks!

It seems that if the decorated function returns a sufficiently large object, a deadlock can occur at the p.join() line, for example:

def will_deadlock()
    return range(30000)

if __name__ == '__main__':
    will_deadlock() # deadlocks here

Simply removing the p.join() line solves the problem. The multiprocessing doc mentioned a possible deadlock issue regarding joining processes that use queues, which seems to be relevant here.


This comment has been minimized.

Copy link

dgerosa commented May 11, 2017


this is incredibly useful! Thanks! You should really upload it to Pypi...



This comment has been minimized.

Copy link

karatheodory commented May 29, 2017

Thank you for the great work, it is very helpful!

One problem I've had with it is when the original exception type has more parameters than usual (in my case it is sqlalchemy.exc.ProgrammingError, which takes 4 arguments, so I've had TypeError: __init__() takes at least 4 arguments (2 given) instead of the original exception). We can go this way:

        if error:
            ex_type, ex_value, tb_str = error
            message = '%s (in subprocess)\n%s' % (ex_value.message, tb_str)
                exception = ex_type(message)
            except Exception:
                # Failed to keep the original exception type
                exception = Exception('%s\n(original exception type: %s)' % (message, ex_type))
            raise exception

This comment has been minimized.

Copy link

samoand commented Aug 26, 2017

Can you please add a license to this code? I was putting together something like that, with splitting input and mapping it to pool of processes. It would be very helpful to use your code as a base. Thanks.


This comment has been minimized.

Copy link

dentonzh commented Jan 30, 2019

Thank you! Solved an issue I've been trying to resolve for the past ~4 hours or so in which my server kills a long-running Python script I was running. Wrapped the offending code in processify and it's taken my CPU / Mem usage down significantly (by a factor of 20-30).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.