[Python-ideas] [Value Returning Threard]

Robert Collins robertc at robertcollins.net
Thu May 7 08:43:44 CEST 2009


On Thu, 2009-05-07 at 06:31 +0000, Antoine Pitrou wrote:
> John Graham <john.a.graham at ...> writes:
> > 
> > If the OP is asking for futures, couldn't that be implemented in a
> > library without touching the language?  It's a powerful enough feature
> > to consider putting it in a library.
> 
> I don't know what C++ futures are but you should take a look at Twisted Deferred
> objects. The official implementation is in Python but a C implementation has
> been lingering on for years, you may be able to give them some help:
> http://twistedmatrix.com/trac/ticket/2245

Futures block, Defereds call forward.

foo = something_returning_a_future() # does not block
foo.get_value() # blocks

def something_returning_a_future():
    def worker():
        [some code here that will does expensive work]
    return Future(worker)

class Future:
    def __init__(callable):
        # imagine thread-safe code here to run callable in a thread

    def get_value(self):
        if not self.result:
            self.result = self.queue.pop()
        return self.result

This is approximately a Future implementation for python. The difference
- and its quite a big one - to Defereds is that defereds are a
call-forward approach, whereas Futures are proxy objects that are waited
on to get their results.

twisted depends on never blocking as a core part of the design, Futures
and Promises don't work well here.

-Rob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090507/f32ae3c0/attachment.pgp>


More information about the Python-ideas mailing list