"Best" way to "drive" a Python program step by step.

Discussion in 'Python' started by André, Sep 15, 2008.

  1. André

    André Guest

    Hi everyone,

    I'd be interested in hearing suggestions as to the "best" way to drive
    a Python program step by step from another application.

    Details:
    ---------

    I have implemented a "Robot" that can be programmed by a user to
    perform certain actions. (see Reeborg below for a simple javascript-
    based example).

    For example, part of a typical program might look like:
    =====
    move()
    turn_left()
    move()
    for i in range(2):
    move()
    ....
    ===

    I had this working in a desktop based application (see RUR-PLE below),
    where the user program was executed via

    exec user_code in MyGlobals

    and MyGlobals contained appropriate definitions. Having it all
    included in a single Python program, I could include time delays in
    each instruction [such as move()], so that the user could stop the
    program running by clicking on a button, etc., and then step through a
    series of instructions one-by-one, and/or resume the automatic
    execution.

    Now, I want to reproduce this behaviour in a browser based application
    (see Crunchy below).

    So, I'd like the Python back end to send information (at set
    intervals) to the browser and be able to react to input from the
    browser (say, a user pressing a "pause" button).

    I could try to reimplement the method I used for RUR-PLE some 3 years
    ago but I know enough now to realize that the method I used was rather
    "inelegant", but don't know enough to see a better way off-hand.

    Any suggestion would be appreciated.

    Cheers,
    André

    Reeborg: http://reeborg.world.googlepages.com/reeborg.html
    Crunchy: http://code.google.com/p/crunchy/
    RUR-PLE: http://rur-ple.sourceforge.net/
     
    André, Sep 15, 2008
    #1
    1. Advertising

  2. André

    Guest

    On Sep 14, 5:41 pm, André <> wrote:
    > Hi everyone,
    >
    > I'd be interested in hearing suggestions as to the "best" way to drive
    > a Python program step by step from another application.
    >


    Andre,

    If you want a Javascript program to render the results of a
    "syncrhronouse" Python program in "real time," with the ability to
    pause, kill, etc., I would suggest a three-tier architecture:

    Javascript client -> Python mediator -> Python target program

    The Python target program would be something like this, written in a
    very synchronous style:

    while True:
    send_command_to_javascript_and_wait('move')
    send_command_to_javascript_and_wait('turnleft')

    The target program would be simple Python code, and the only minor
    magic would be in the send_command_to_javascript_and_wait method,
    which would normally just call "print," but when running as a
    subprocess, would block until the parent indicates it should proceed.

    I think it's useful, especially on this mailing list, to think about
    how the Python "mediator" program controls the Python "target"
    program. Essentially you want the "target" program to be a subprocess
    of the "mediator" program. You will probably want to use the
    "Subprocess" module. Look at the "Reading Output of Another Command"
    section of the article below:

    http://www.oreillynet.com/onlamp/blog/2007/08/pymotw_subprocess_1.html

    Once you solve the problem of having another Python program control
    your "target" program, it's not too big a conceptual leap to have a
    Javascript (JS) program control it. I would suggest the following
    high level architecture:

    1) JS client communicates with Python mediator via HTTP Ajax calls.
    2) JS can send HTTP request to mediator to have it start the target
    as a subprocess.
    3) JS can send HTTP request to mediator to have it deliver data to
    target subprocess.
    4) Target subprocess can send data back to mediator that is to be
    part of the HTTP response.

    In your rur-ple application data will be flowing mostly in one
    direction. The JS client will simply be telling the Python target to
    advance a step, while the Python target will be giving specific
    metadata about how to update the world that JS renders. Still,
    despite the asymmetrical nature of your app, I would try to think of
    it as a two-way exchange of data. The JS program is communicating
    what the user wants to do next (usually a generic request to execute
    the next instruction), and the Python backend is communicating how to
    change the view (usually as a builtin command like move_robot or
    highlight_line_of_code), but ultimately it's all data.

    Hope that helps!

    Cheers,

    Steve
    http://webstervanrobot.com/
     
    , Sep 15, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon
    Replies:
    5
    Views:
    11,181
    Scott M.
    Jan 17, 2010
  2. mh
    Replies:
    4
    Views:
    706
    Wolfgang Strobl
    May 31, 2005
  3. Claudio Grondi
    Replies:
    2
    Views:
    361
    Claudio Grondi
    Jul 3, 2006
  4. shailesh
    Replies:
    1
    Views:
    793
    Tim Golden
    Mar 28, 2007
  5. davy zhang
    Replies:
    1
    Views:
    461
    Aaron Brady
    Nov 8, 2008
Loading...

Share This Page