[IPython-dev] Musings: syntax for high-level expression of parallel (and other) execution control

Fernando Perez fperez.net at gmail.com
Tue Sep 8 16:34:33 EDT 2009


On Tue, Sep 8, 2009 at 10:23 AM, Brian Granger <ellisonbg.net at gmail.com> wrote:
> My only complaint is that is a bit unexpected that this actually declares
> and *calls* the function!

Yes, it is more than a bit surprising at first :)  In fact, I remember
that bothered me about @interact when I first saw it, and that was
partly why I tried to make things work using 'with'.  But it's now
clear to me that we need  a *real* scope for these ideas, and for now,
'def' is the only way to get a scope we have, so that's what we'll be
using.

For reference, the stuff I was trying to implement from the Ars review
is below in the original Objective C code, plus my own 'pythonization'
of it, first in serial mode, then using 'with', then with @decos.

This is the page in the review where John Siracusa explains the new
Block syntax that Apple introduced to C:

http://arstechnica.com/apple/reviews/2009/08/mac-os-x-10-6.ars/10

I think it's really cool, I do hope it makes its way into the language itself.

Cheers,

f

### Code
from __future__ import with_statement
"""
http://arstechnica.com/apple/reviews/2009/08/mac-os-x-10-6.ars/13

This is Objective C code, taken from the link above:

- (IBAction)analyzeDocument:(NSButton *)sender
{
  NSDictionary *stats = [myDoc analyze];
  [myModel setDict:stats];
  [myStatsView setNeedsDisplay:YES];
  [stats release];
}

- (IBAction)analyzeDocument:(NSButton *)sender
{
  dispatch_async(dispatch_get_global_queue(0, 0), ^{
    NSDictionary *stats = [myDoc analyze];
    dispatch_async(dispatch_get_main_queue(), ^{
      [myModel setDict:stats];
      [myStatsView setNeedsDisplay:YES];
      [stats release];
    });
  });
}
"""

def analyzeDocument():
    """A Python version of the serial version above"""
    stats = myDoc.analyze()
    myModel.setDict(stats)
    myStatsView.setNeedsDisplay(YES)
    stats.release()

def analyzeDocument():
    """A hypothetical code using 'with'.

    This type of hack is not only ugly, but extremely brittle (to actually
    run, it has to do all kinds of nasty stack manipulations.  It is meant
    only to illustrate what the syntax could look like. """

    with dispatch_async(dispatch_get_global_queue(0, 0)):
        stats = myDoc.analyze()
        with dispatch_async(dispatch_get_main_queue()):
            myModel.setDict(stats)
            myStatsView.setNeedsDisplay(YES)
            stats.release()

def analyzeDocument():
    """A decorator-based version.

    This could in principle work just fine today.  All one would need to do
    would be to write Python decorators for Apple GCD.  As long as the code
    being run released the GIL, it would work fine. """

    @dispatch_async(dispatch_get_global_queue(0, 0))
    def outer():
        stats = myDoc.analyze()
        @dispatch_async(dispatch_get_main_queue())
        def inner():
            myModel.setDict(stats)
            myStatsView.setNeedsDisplay(YES)
            stats.release()



More information about the IPython-dev mailing list