[Python-ideas] Decorators on loops

Haoyi Li haoyi.sg at gmail.com
Wed Jan 8 20:05:39 CET 2014


> The problem of a loop being that its semantics are too generic to make
anything even remotely close to such an assumption.

I think it's the opposite problem, really: the semantics (repeatedly
calling .next() on iter(...)) is too specific, and is incompatible with
what you want. What you want is a generic map() function which encodes the
semantics (independent updates) that you want, and map() is trivially
parallelizable.


On Wed, Jan 8, 2014 at 10:53 AM, Masklinn <masklinn at masklinn.net> wrote:

> On 2014-01-08, at 18:47 , Enric Tejedor <enric.tejedor at bsc.es> wrote:
> > Correct, this is indeed a problem. It would be tricky to make this work
> > in the general case.
> >
> > In a simpler scenario, we could assume that iterations won't update the
> > same data.
> > On the other hand, to prevent the UnboundLocalError, the variables
> > needed inside the loop could be passed to the decorator and appear in
> > the loop function's signature.
> >
> > results = [0] * 10
> >
> > @parallel(range(10), results)
> > def loop(i, results):
> >       results[i] = some_computation(i)
>
> At this point you don't really need a decorator anymore, this is an
> odd-ish way to write `results = map(some_computation, range(10))`, and
> as others have noted the standard library already has a parallelized
> version thereof:
>
> http://docs.python.org/2/library/multiprocessing.html#multiprocessing.pool.multiprocessing.Pool.map
>
> > Then the decorator would be:
> >
> > def parallel(*args):
> >   iterable = args[0]
> >   params = args[1:]
> >
> >   def call(func):
> >       # create parallel invocations of func with iterable and params
> >
> >   return call
> >
> > I think this solution would work if you wanted to do things like
> > performing independent updates on a list.
>
> The problem of a loop being that its semantics are too generic to make
> anything even remotely close to such an assumption.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140108/fd47cdcc/attachment.html>


More information about the Python-ideas mailing list