Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > "Best" way to "drive" a Python program step by step.

Thread Tools

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

Posts: n/a
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.


Reply With Quote
Posts: n/a
On Sep 14, 5:41*pm, André <(E-Mail Removed)> 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!


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to step debug the multiprocessing python program? davy zhang Python 1 11-08-2008 03:28 AM
wanted: framework for creating nice step by step graphical visualisationsof running Python code Claudio Grondi Python 2 07-02-2006 11:55 PM
Step By Step Instructions Glenallan Wireless Networking 15 12-29-2004 12:13 AM
can somebody guide me through this step by step? Taquin Noel Wireless Networking 5 12-26-2004 01:31 AM
step by step loading a design into flash with nios excalibur jaap de verwant slachter VHDL 0 07-01-2003 01:02 PM