[Python-ideas] @run_as_thread decorator

Antoine Pitrou solipsis at pitrou.net
Sat Mar 5 15:50:10 CET 2011


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...).

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.

Regards

Antoine.





More information about the Python-ideas mailing list