[Python-ideas] @run_as_thread decorator

Giampaolo Rodolà g.rodola at gmail.com
Sat Mar 5 20:25:20 CET 2011


It is probably a bad idea, I don't know. I'm the first one being
skeptical about it.
Maybe a real world example can bring some contribution to this
discussion since using this kind of approach at module-level doesn't
bring real benefits over using threading.Thread(target=func).start().

In my experience, I've found this extremely elegant when I was
developing a web application in which I wanted to start a serie of
long running tasks involving the db, and didn't care about the
function return value, nor I wanted to return anything relevant to the
user other than a simple "task started - this may take some time"
message.
The code looked like this:


from utils import run_as_thread

class Admin:

    @run_as_thread
    def refresh_persons(self):
        ...

    @run_as_thread
    def refresh_voyages(self):
        ...

    @run_as_thread
    def refresh_addresses(self):
        ...


> Basically you are proposing to complicate the API for no real benefit
> except that it "feels good".

I'd say there's a benefit in terms of elegance if this is used in a certain way.
On the other hand I understand your complaints, and this probably fits
better in an "util" module rather than threading.
I like the function return value example proposed by Bruce.
Is there a reason why this is not provided by base Thread class?

http://code.activestate.com/recipes/84317/
...and:
http://www.google.com/#sclient=psy&hl=en&q=python+thread+return+value&aq=f&aqi=g1&aql=&oq=&pbx=1&bav=on.2,or.&fp=369c8973645261b8

...suggest that users tend to require this feature.


--- Giampaolo
http://code.google.com/p/pyftpdlib/
http://code.google.com/p/psutil/


2011/3/5 Antoine Pitrou <solipsis at pitrou.net>:
> On Sat, 5 Mar 2011 19:00:52 +0100
> Giampaolo Rodolà <g.rodola at gmail.com>
> wrote:
>>
>> I don't know whether it is a good idea to provide such a thing
>> natively, but I can't even figure out what exactly is wrong/weird with
>> this exactly.
>
> I don't understand what this two line variant brings over the other
> two-line variant:
>
>  t = threading.Thread(target=func)
>  t.start()
>
> Basically you are proposing to complicate the API for no real benefit
> except that it "feels good". It also makes things harder to learn for
> beginners since there are two abstractions stacked one over the other.
> It doesn't sound like a sensible addition.
>
> Regards
>
> Antoine.
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



More information about the Python-ideas mailing list