[PEAK] Re: Trellis-fork

Sergey Schetinin maluke at gmail.com
Fri Jul 17 15:23:51 EDT 2009


I've just commited a support for deferred results / exceptions.

I was using it for a while and just moved it to Reaction from a
private library. The purpose of Deferreds in Reaction is very similar
to their purpose in Twisted -- to wait for a result that will be set
by some other process.

In Reaction they are used from @tasks. To wait for a deferred, you do

   val = yield dfd

That's the same as waiting for a subtask except it's easier to set
from some other code, e.g. a worker thread: dfd.set(result) or
dfd.set_error()

One interesting feature is waiting for multiple deferreds to finish:

    v1,v2,v3 = yield (d1|d2|d3)

For example, imagine you're downloading a webpage, and you want to
wait until all images load, instead of downloading them one by one
you'd start all those processes and wait for them. And if any of them
fail, you get that exception immediately.

Now, another interesting addition is @task_method, it's kinda like
@task but it has to be called to start, it can have arguments and it
will start as many tasks as many times it will get called, for
example:

    @task_method
    def fetch_url(self, url):
        ...
        conn = yield self.get_conn(host)
        data = yield conn.download(path)
        yield data

One would think, why not just drop the decorator and use the generator
as a subtask? Well, because one can't wait for multiple subtasks and
@task_method wrapped calls return Deferreds! Also, only one task can
wait for a subtask, there is no limit as to how many tasks can be
waiting / getting the result of the same deferred (unlike Twisted btw)

   wait = wait_all()
   for url in image_urls:
       wait |= self.fetch_url(url)
   images = yield wait
   # now images is a list of strings -- url data
   img_url2data = dict(zip(image_urls, images))


For more, see docstrings and source code:
http://code.google.com/p/trellis-fork/source/browse/trunk/mext/reaction/deferred.py




-- 
Best Regards,
Sergey Schetinin

http://s3bk.com/ -- S3 Backup
http://word-to-html.com/ -- Word to HTML Converter


More information about the PEAK mailing list