473,320 Members | 2,202 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,320 software developers and data experts.

pty difficulties

I'm writing a utility that runs a program inside of a pseudo-tty and
then multiplexes both stdin and a fifo to the child's pty via a select
loop.

In other words, you run my program, tell it the name of the program to
run and the name of the fifo to use and then you can interact with the
program as per usual. However, you can also send data into the fifo
and the program will see it as input just as if you'd typed it on the
keyboard.

As a test I'm running sbcl (a lisp interpreter) through my python
program. It works like a charm in normal usage. However, if I dump
more than 2K or so of code into the FIFO, sbcl gets the first K fine,
but then seems to miss a chunk, get another character or two and then
freeze. After that I am unable to interact with it via either stdin
or the fifo even though the select loop is still running. The pty
just seems to be dead, eating input and giving nothing back. So, I
tried running a different Lisp interpreter (openmcl) instead, but the
problem persisted. So, I just ran vim through it and it handled the
2K of text just fine.

However, vim is just sending the text into a buffer where-as sbcl is
parsing and compiling it. If I add a sleep(1.0) after data is sent
from the fifo to the pty then the problem goes away. So, it seems
like if the child process can't pull things out of it's stdin as fast
as I'm putting it in, something goes awry.

Has anyone seen similar behavior? Does anyone have any idea why this
would be happening?

Any help would be greatly appreciated. Thanks everyone. The code is
below.

Justin Dubs
Here's the code:

from time import sleep
from pty import spawn, fork
from sys import stdin, stdout, stderr, exit, argv
from os import fdopen, open, read, write, O_RDONLY, O_NONBLOCK
from select import select
from termios import tcgetattr, tcsetattr, tcdrain, ECHO, TCSADRAIN
from tty import setraw

if (len(argv) != 3):
print "usage: vee.py program pipe"
exit(0)

pid, childID = fork()

if pid == 0:
spawn(argv[1])
else:
fifo = fdopen(open(argv[2], O_RDONLY | O_NONBLOCK), 'r')
child = fdopen(childID, 'r+')

attribs = tcgetattr(stdin)
attribs[3] = attribs[3] & ~ECHO
tcsetattr(stdin, TCSADRAIN, attribs)

setraw(stdin)

connections = { child : stdout, stdin : child, fifo : child }

try:
while True:
readable = select(connections.keys(), [], [])[0]
for f in readable:
data = read(f.fileno(), 1024)
connections[f].write(data)
connections[f].flush()
except (OSError, IOError):
exit(0)
Jul 18 '05 #1
3 1515
> while True:
readable = select(connections.keys(), [], [])[0]
for f in readable:
data = read(f.fileno(), 1024)
connections[f].write(data)
connections[f].flush()


I believe your problem exists in the write and flush. All you seem to
be doing is checking to see if your reading file handles are capable of
reading, you never check to see if you can write to anything. Your lisp
interpreter showcases the fact that it is not ready to get a write while
it is interpreting, by failing. I believe the following should fix you up:

while True:
readable = select(connections.keys(), [], [])[0]
for f in readable:
if select([], [connections[f]], [], 0)[1]:
data = read(f.fileno(), 1024)
connections[f].write(data)
connections[f].flush()

- Josiah
Jul 18 '05 #2
Josiah Carlson <jc******@nospam.uci.edu> wrote in message news:<bu**********@news.service.uci.edu>...
while True:
readable = select(connections.keys(), [], [])[0]
for f in readable:
data = read(f.fileno(), 1024)
connections[f].write(data)
connections[f].flush()


I believe your problem exists in the write and flush. All you seem to
be doing is checking to see if your reading file handles are capable of
reading, you never check to see if you can write to anything. Your lisp
interpreter showcases the fact that it is not ready to get a write while
it is interpreting, by failing. I believe the following should fix you up:

while True:
readable = select(connections.keys(), [], [])[0]
for f in readable:
if select([], [connections[f]], [], 0)[1]:
data = read(f.fileno(), 1024)
connections[f].write(data)
connections[f].flush()


Thanks for the suggestions, but this didn't help. I should have
mentioned that I had tried this before. Here's the code I had used:

while True:
readable, writeable, ignore = select(connections.keys(),
connections.values(), [])
for f in readable:
if connections[f] in writeable:
data = read(f.fileno(), 1024)
connections[f].write(data)
connections[f].flush()

I don't think checking for writeable status /should/ help because the
write call I am using blocks. So, if the stream isn't writeable, it
should just block until it is. I think.

Justin Dubs
Jul 18 '05 #3
> I don't think checking for writeable status /should/ help because the
write call I am using blocks. So, if the stream isn't writeable, it
should just block until it is. I think.


Hrm. Perhaps you should just leave the sleep in there. One second
seems a bit much, but maybe .01 or .001 seconds is sufficient. You
could reasonably still get 100K-1M/second. One would hope that would be
enough for most tasks.

- Josiah
Jul 18 '05 #4

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

Similar topics

2
by: Yannick Turgeon | last post by:
Hello all, I have a web page in which I create a simple PNG picture using PHP. It was correctly displayed until I upgrade my PC (Linux/Mandrake 9.1). Now my little picture is a black square! ...
1
by: Max B-K | last post by:
I've delved into the usage of the PHP Tokenizer that directly interfaces with the Zend engine. So far, I have found it incredibly useful when it comes to editing a PHP file. What I am trying...
0
by: Randi | last post by:
Hi all, I have this project to use the ADODB control to acces and manipulate the Access DB. I amd the mistake of first doing this project with just the data control. It worked fine with this...
7
by: Bix | last post by:
As this is my very first post, I'd like to give thanks to all who support this with their help. Hopefully, this question hasn't been answered (too many times) before... If anyone could explain...
1
by: Chris Lasher | last post by:
Hello, I'm trying to write a tool to scrape through some of the Ribosomal Database Project II's (http://rdp.cme.msu.edu/) pages, specifically, through the Hierarchy Browser....
2
by: brolewis | last post by:
I am trying to deploy Python onto a number of laptops and have been trying to take advantage of Python 2.4's MSI installer. I have tried using the following commands to install, but to no avail: ...
0
by: Julia | last post by:
Hi, I am still having Charest conversion difficulties s when passing string from C# TO ASP and than to access using ADO I am using HttpWebRequest to POST some Multilanguage(Hebrew and...
3
by: jg.campbell.ng | last post by:
I'm beginning learning Python and OpenGL in Python. Python fine. But difficulties with OpenGL; presumably with the installation of OpenGL. OS = Linux FC5. Python program gl_test.py: from...
1
by: =?Utf-8?B?c3R1bXBlZA==?= | last post by:
Am having difficulties opening attachments with a ".email" extension - how do i do this - i can open them in my web-based e-mail, but not Outlook Express. Thank you
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.