future multi-threading for-loops

castironpi at gmail.com castironpi at gmail.com
Tue Feb 5 19:15:04 EST 2008


On Feb 5, 1:21 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Mon, 04 Feb 2008 19:22:29 -0800, castironpi wrote:
> > Some iterables and control loops can be multithreaded.  Worries that
> > it takes a syntax change.
>
> > for X in A:
> >     def f( x ):
> >         normal suite( x )
> >     start_new_thread( target= f, args= ( X, ) )
>
> > Perhaps a control-flow wrapper, or method on iterable.
>
> > @parallel
> > for X in A:
> >     normal suite( X )
>
> > for X in parallel( A ):
> >     normal suite( X )
>
> > Discussion presued about multi-core systems.  Allow user certain
> > control over what runs on multi-core.  Clearly, not generally
> > applicable.  -- But, from __future__ import does change syntax.
>
> Why not simply writing a function?
>
> def execute_parallel(f, A):
>     for args in A:
>         start_new_thread(target=f, args=args)
>
> def f(x):
>     normal_suit(x)
>
> parallel(f, A)
>
> Ciao,
>         Marc 'BlackJack' Rintsch- Hide quoted text -
>
> - Show quoted text -

Are code blocks first-class objects, without customizing syntax?

from functools import partial
from threading import Thread
from random import uniform

A= range(10)

@partial( partial, partial )
def execute_parallel( f, A ):
    for args in A:
        Thread( target= f, args= ( args, ) ).start()

from time import sleep

@execute_parallel( A= A )
def f(x):
    sleep( uniform( 0, .2 ) )
    print(x)

[Windows XP threading timing code, 10,000 calls]: [ allocate,
CreateEvent, CreateThread, WaitForMultipleObjects ]: 0.887416 secs



More information about the Python-list mailing list