pty/select/timers/SIGINT ?s w/ embedded python in vim

Brian Sturk bsturk at news1.news.adelphia.net
Mon Apr 8 02:22:41 EDT 2002


I've written a python script for vim that allows you to run a shell
within a buffer using the pty module.  I've got most of what I wanted
to working but there are still 2 issues remaining that make using
it somewhat annoying.  I've been trying to get these working for
quite a while and haven't made any inroads on them at all.  I'm
hoping someone might have an idea or two...

If any of this is too vague or I'm not making sense I can post
code snippets or, you can check out the script:

    http://nh.ultranet.com/~bsturk/programs/vimsh.tar.gz

1.  Reading output from slow commands (ftp) or commands that have constant
    output until interrupted (ping):

    I need a way to be able to read output in the background, but
    unfortunately I can't use threads.  I also couldn't find a timer
    type module ( I did find Timer, which seems to use threads ).

    Here's an example scenario...

    The vim user executes a command, for instance, ftp.

    I use select to see if there's any output and depending on how
    long it takes I may or may not get something.
    
    If my timeout to select is short I sometimes miss the data but
    commands that aren't slow aren't hung up on on their last read.
    
    If the timeout is greater I have a better chance of reading
    in data from slow commands but other commands appear sluggish
    when reading the last bits from stdout.

    Maybe I'm approaching this the wrong way.  I had contemplated doing
    some kind of process watching so I could be more intelligent about
    whether to be reading or not.  I've run out of ideas on this one.

    I've got a workaround for this by mapping a key to check for
    more output.

2.  SIGINT handling:

    I wrap executing a shell command w/ a try, except KeyboardException and
    correctly catch the exception when the user presses ctrl-c.  I then
    send whatever key sequence the user has set for EINTR out to the
    running process, so something like this:

        os.write( self.ind, intr_key )

    self.ind in this case is the file descriptor returned from
    pty.fork( ), and intr_key might be something like '^C'.

    This works in that the executed command is interrupted but also
    seems to trigger another KeyboardException in the python script. 
    Does this have something to do w/ process groups or something like
    that?  I would have thought that only the process that 'received' it would
    'get' it.  What this is keeping me from doing is reading any left
    over data, like ping's statistics or a new prompt.

    Again the workaround is to use the key mapping to check for more
    output.

    Any help on these would be greatly appreciated.

-- 
.---------------------------------------------------,---------.  
| Brian Sturk - http://www.nh.ultranet.com/~bsturk  \  C/C++  | .>   )\,^a__
|--------------------------|  bsturk at nh.ultranet.com | Python |(  _ _)/ /-." ~
| http://www.telengard.com `-------------------------`--------| `( )_ )/
| Telengard Technologies Inc. -  NT/*nix UI & device drivers  |_<_s_<_s
'-------------------------------------------------------------'



More information about the Python-list mailing list