[Python-ideas] Decorators on loops

Masklinn masklinn at masklinn.net
Wed Jan 8 19:53:11 CET 2014


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.



More information about the Python-ideas mailing list