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

showellshowell at gmail.com showellshowell at gmail.com
Mon Sep 15 03:50:22 CEST 2008

On Sep 14, 5:41 pm, André <andre.robe... at gmail.com> 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!



More information about the Python-list mailing list