[Python-ideas] @run_as_thread decorator

Georg Brandl g.brandl at gmx.net
Sat Mar 5 16:56:21 CET 2011

On 05.03.2011 15:50, Antoine Pitrou wrote:
> On Sun, 6 Mar 2011 00:10:56 +1000
> Nick Coghlan <ncoghlan at gmail.com> wrote:
>> So basically:
>> def run_as_thread(f):
>>     @functools.wraps(f):
>>     def wrapped(*args, **kwds):
>>         t = threading.Thread(target=f, args=args, kwds=kwds)
>>         t.start()
>>         return t
>>     return wrapped
>> Something like that would make defining worker threads *really* easy.
> I don't really agree.
> First, as you guess, there's already a rather obvious one-liner:
>   threading.Thread(target=f).start()
> Second, any decorator that implicitly spawns a thread is a very bad
> idea (especially when used at module level...).

It doesn't spawn the thread on definition, but on function call time.
That's not as bad, but I agree that it is too magical for the stdlib.

> I'm rather opposed to this, it's a useless addition to the API with no
> real point. Calling the Thread() constructor works basically ok.

Problem is, the one-liner doesn't give you a reference to the Thread object.


More information about the Python-ideas mailing list