473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Debugging pipe IPC

I have the mother (of all) application(s, written in C++) that
occasionally outsources certain tasks to a child Python script. The
mother fork/execs (or equivalent) the child and then begins serving the
child's requests.

The child/client sends requests on its stdout and receives responses on
stdin. The interaction is facilitated by a Pyrex extension which handles
the lower-level aspects of the conversation and exposes a few functions
and classes to the script.

This part works peachy keen, but debugging has so far been via
print>>stderr-and-scratch-head.

On a contemplative bike ride one day, I imagined how neat it would be to
run interactively. On returning, I began to experiment with rebinding
sys.stdin and sys.stdout to /dev/tty, using the "-i" command-line switch,
etc. to see if I could devise a child that prompted me with ">>>" and
allowed me to compose/test small snippets on the terminal. So far, no
joy.

Is this possible? If so, can someone nudge me toward a solution or
better yet a recipe?

Jim Wilson
Gainesville, FL

Dec 17 '07 #1
8 1622
Jim B. Wilson wrote:
I have the mother (of all) application(s, written in C++) that
occasionally outsources certain tasks to a child Python script. The
mother fork/execs (or equivalent) the child and then begins serving the
child's requests.

The child/client sends requests on its stdout and receives responses on
stdin. The interaction is facilitated by a Pyrex extension which handles
the lower-level aspects of the conversation and exposes a few functions
and classes to the script.

This part works peachy keen, but debugging has so far been via
print>>stderr-and-scratch-head.

On a contemplative bike ride one day, I imagined how neat it would be to
run interactively. On returning, I began to experiment with rebinding
sys.stdin and sys.stdout to /dev/tty, using the "-i" command-line switch,
etc. to see if I could devise a child that prompted me with ">>>" and
allowed me to compose/test small snippets on the terminal. So far, no
joy.

Is this possible? If so, can someone nudge me toward a solution or
better yet a recipe?

Jim Wilson
Gainesville, FL
You're looking for the cmd module.
<http://docs.python.org/lib/module-cmd.html>

Ian Clark

Dec 17 '07 #2
Ian Clark pointed me to:
... the cmd module.
<http://docs.python.org/lib/module-cmd.html>
Yes, I found that, but I could only get it to print a nice interactive
prompt, "(Cmd)", read a line of input and discard it. Apparently, I'm
too stupid to figure out how to hook it into python.

Jim
Dec 17 '07 #3
Jim B. Wilson wrote:
....
The child/client sends requests on its stdout and receives responses on
stdin.
So, why can't you just run this client on the command line and let the
shell handle stdin/stdout for you?

Ian Clark

Dec 17 '07 #4
Ian Clark wrote:
Jim B. Wilson wrote:
...
>The child/client sends requests on its stdout and receives responses
on stdin.

So, why can't you just run this client on the command line and let the
shell handle stdin/stdout for you?
I'm not sure I understand the topology of your proposal? And, it's
certainly possible the shell has mystical powers of which I am unaware.

Are you suggesting a run client that spews the mother's messages to the
terminal, and I somehow cut/paste them into the client, running with
stdin/stdout unconnected to anything but me? Or is there some
combination of tee, etal., I can lash together so that my commands (to
the client) don't get mixed up with mother's messages?

Some such arrangement would surely help. I often forget the ">>stderr,"
on my scratch-head debugging prints, and mother gets quite upset when
the incoming message doesn't fit the mold :)

Jim
Dec 17 '07 #5
Jim B. Wilson wrote:
Ian Clark wrote:
>Jim B. Wilson wrote:
...
>>The child/client sends requests on its stdout and receives responses
on stdin.
So, why can't you just run this client on the command line and let the
shell handle stdin/stdout for you?

I'm not sure I understand the topology of your proposal? And, it's
certainly possible the shell has mystical powers of which I am unaware.

Are you suggesting a run client that spews the mother's messages to the
terminal, and I somehow cut/paste them into the client, running with
stdin/stdout unconnected to anything but me? Or is there some
combination of tee, etal., I can lash together so that my commands (to
the client) don't get mixed up with mother's messages?

Some such arrangement would surely help. I often forget the ">>stderr,"
on my scratch-head debugging prints, and mother gets quite upset when
the incoming message doesn't fit the mold :)

Jim
What I'm suggesting is to run the following from the shell: python
internal_python_program.py. Then you type into the terminal whatever
'mother' was sending it ("Clean your room!" most like) and see what it
spits back. If the script is simply reading from stdin and writing to
stdout there shouldn't be much of a problem. The hardest part might be
figuring out what 'mother' actually sends it.

If it's very verbose you can type it out once to a file and redirect
that file to the python process: python internal_python_program.py <
file_with_mother_commands.txt.

Another idea is to open a file somewhere and write your debug output to
that. I would also suggest the logging[1] module as well. It's very
handy for his sort of thing.

Ian

[1] http://docs.python.org/lib/module-logging.html

Dec 18 '07 #6
Ian Clark wrote:
... whatever 'mother' was sending it ("Clean your room!" most like)
:)
If it's very verbose ...
Alas, it is quite verbose. Constructing a single instance of a class
(from the Pyrex extension acting as the child's two-way radio) could
involve tens of thousands of more-or-less random exchanges between
mother and child. A subsequent method call could trigger a similar
volume of traffic.

And it isn't this communication I'm worried about. All that seems to go
quite well. My fondest wish is to play the role of the child at the
good old ">>>" prompt. Such play would allow me to test tiny snippets
of code that I could later incorporate into an actual child.

My wish may not be possible. If so, it's no great tragedy. A simple
test goes from:
>>foo = askmommy()
foo.why()
to:

1. Typing similar statements into my editor.
2. Saving these statements to a file, say "test.py".
3. Making clicky-clicky in the mother application to run "test.py".
4. Noticing that nothing appears on my terminal.
5. Inserting ">>sys.stderr," into "print foo.why()" :)
6. Repeating steps 2 and 3.

Steps 4, 5 and 6 are required because the mother has absolutely no
compunction against infanticide when her child asks her a question she
cannot answer.

If my wish isn't possible, it will be the first time. Normally, when I
wonder if Python can do something, I imagine what the syntax would be if
it could, type it in, and sure enough it works.

Jim
Dec 18 '07 #7
Jim B. Wilson wrote:
... My fondest wish is to play the role of the child at the
good old ">>>" prompt. ...

Jim
Okay, I misunderstood the direction you wanted to go. I thought that you
wanted to play the part of the mother, giving commands to the child and
not the other way around.

Assuming you're on a *nix make child.py as:

import os
os.system("netcat -l -p 1234 localhost")

Run it though mother and on another terminal type: netcat localhost 1234

HTH,

Ian

Dec 18 '07 #8
Ian Clark wrote:
import os
os.system("netcat -l -p 1234 localhost")

HTH,
Nope, but the network theme got me thinking about how one might run
Python on a remote host. After a few false starts, Googling "remote
python shell" led me to Guido's "ripshell.py" (not *that* Guido, a
different one). Peeking inside, I discovered the module I'm looking for
was "code". Remember, in my case, communication on stdin and stdout
was all handled by a Pyrex extension. I'm unsure the code below will
work in a pure Python application.

For posterity, here is itest.py, the solution to my problem:

### itest.py - allows interactive debugging of a "filter"
#
# If stdin and stdout are dedicated to your filter script, but you
# want to interactively check a few things, try something like this:

from code import interact
import sys

sys.stdout = open("/dev/tty", "w") # Rebind Python's lips
sys.stdin = open("/dev/tty", "r") # and Python's ears.

foo = "This is a test" # Lay down some history
interact( # Convenience function from code.
"At your service, Sir!", # BUG: could be "Madam" or "Miss"
local = locals()) # Teach interpreter some history
print "Thank you, Sir!" # BUG: ibid.

To check this works:

$ python itest.py </dev/null >/dev/full # mother is a deafmute
At your service, Sir!
>>foo
'This is a test'
>>^D (not shown)
Thank you, Sir!
$
Dec 20 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
1
by: jenny | last post by:
Hi, I have a java socket program running on AIX 4.3.3.0 platform. It opens a socket and sends data to our customer over a leased fractional T1 line. The line is always connected. However,...
5
by: richard | last post by:
I have a simple test to pass information from a client to a server using named pipe. what I really want is: when I type a line on the client, the server will output the line immediately. but to my...
7
by: Greg | last post by:
I am trying to implement the UNIX pipe command using C but with the "->" operator. Everything works fine with 1 pipe, but when I try to use 2 or more, it hangs up when reading the pipe_in...
14
by: | last post by:
I cannot for the life of me get remove debugging to work. I continue to receive "Error while trying to run project: Unable to start debugging on the web server. Access is denied. Verify that you...
2
by: FB's .NET Dev PC | last post by:
I am writing two services in VB.NET, one of which needs to send text strings to the other. After reading, I decided (perhaps incorrectly) that named pipes would be the best interprocess...
2
by: Steve R. Hastings | last post by:
While studying iterators and generator expressions, I started wishing I had some tools for processing the values. I wanted to be able to chain together a set of functions, sort of like the...
2
by: Samuel | last post by:
Hi, When using telnetlib, the connection sometimes breaks with the following error: "error: (32, 'Broken pipe')" where the traceback points to self.sock.send(buffer)
0
by: carl.dhalluin | last post by:
Hi I want a simple way to interactively remote debug a running python script which has no tty terminal attached to it. The scripts are running standalone (i.e. they are automatically started and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.