[Python-ideas] Decorators for running a function in a Process or Thread

NoxDaFox noxdafox at gmail.com
Mon May 1 07:13:00 EDT 2017

The decorators would abstract the logic of spawning a process or thread and
maintaining its lifecycle.

I think it could be a good fit for the `concurrent.futures` module.
Decorated functions would return a `Future` object and run the logic in a
separate thread or process.

def function(arg, kwarg=0):
    return arg + kwarg

future = function(1, kwarg=2)

The Process decorator in particular would support use cases such as running
unstable code within an application taking advantage of process separation
I often found myself relying on external APIs which would either crash or
run indefinitely affecting my application stability and performance.

def unstable_function(arg, kwarg=0):
    # hang or segfault  here
    return arg + kwarg

future = unstable_function(1, kwarg=2)
except TimeoutError as error:
    print("Function took more than %d seconds" % error.args[1])
except ProcessExpired as error:
    print("Process exit code %d" % error.exitcode)

In case of timeout, the process would be terminated reclaiming back its

Few years ago I wrote a library for this purpose which turned out pretty
handy in reducing the boilerplate code when dealing with threads and
processes. It could be a starting point for discussing about the
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170501/e938094a/attachment.html>

More information about the Python-ideas mailing list