[Python-ideas] @run_as_thread decorator

geremy condra debatem1 at gmail.com
Sat Mar 5 20:32:15 CET 2011


On Sat, Mar 5, 2011 at 10:37 AM, Bruce Leban <bruce at leapyear.org> wrote:
> If there's going to be support for turning a function into a thread, I think
> it should still be useful as a function. Here's how I'd change the original
> proposal here:
>
>>>> @threading.run_as_thread
> ... def foo():
> ...     time.sleep(100)
> ...     return 1
> ...
>>>> t = foo()
>>>> t.result()
> None
>>>> t.isAlive()
> True
>>>> t.join()
>>>> t.isAlive()
> False
>>>> t.result()
> 1
>>>> foo().result(join=True)
> 1
>>>> foo().result(join=True, timeout=1)
> None
> # as with Thread.join, in this case you cannot tell from the return value if
> # the join happened (unless you know the function cannot return None)

Is there a reason that a dedicated sentinel value can't be used here?

Geremy Condra



More information about the Python-ideas mailing list