By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,683 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

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

P: 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.

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/
Sep 15 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sep 14, 5:41*pm, André <andre.robe...@gmail.comwrote:
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/blo...process_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 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.