"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.


    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:
    for i in range(2):

    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

    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.


    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. Advertisements

  2. 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.


    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:

    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:


    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!


    , Sep 15, 2008
    1. Advertisements

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
    Scott M.
    Jan 17, 2010
  2. mh
    Wolfgang Strobl
    May 31, 2005
  3. Claudio Grondi
    Claudio Grondi
    Jul 3, 2006
  4. shailesh
    Tim Golden
    Mar 28, 2007
  5. davy zhang
    Aaron Brady
    Nov 8, 2008

Share This Page