Language Enhancement Idea to help with multi-processing (your opinions please)

alex23 wuwei23 at gmail.com
Fri Oct 14 22:32:35 EDT 2011


On Oct 13, 10:35 pm, "Martin P. Hellwig" <martin.hell... at gmail.com>
wrote:
> def do_something():
>      a = 4
>      b = 2
>      c = 1
>      ooo:
>          a += 1
>          b += 2
>          c += 3
>      print(a, b, c)
>
> What I would expect to happen that all statements within the ooo block
> may be executed out
> of order. The block itself waits till all statements are returned before
> continuing.
>
> What do you think?

You can do this right now with Python 3.2+ and concurrent.futures:

from concurrent.futures import ThreadPoolExecutor
from functools import partial
import time

class DoSomething:
    a = 4
    b = 2
    c = 1

    def add(self, prop, val):
        cur = getattr(self, prop)
        time.sleep(cur)
        print('Adding %d to %s' % (val, prop))
        setattr(self, prop, cur + val)

    def __init__(self):
        with ThreadPoolExecutor(max_workers=3) as pool:
            pool.submit(self.add, 'a', 1)
            pool.submit(self.add, 'b', 2)
            pool.submit(self.add, 'c', 3)
        print(self.a, self.b, self.c)

DoSomething()

Here we call 'ooo' the ThreadPoolExecutor context manager :)



More information about the Python-list mailing list