Jump to content

Piping a program.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Somelauw

Somelauw

    Newbie

  • Members
  • PipPip
  • 18 posts
I have a file called increment.py as follows:
#!/usr/bin/python
n = 0
while True:
        n = int(raw_input(n)) + 1

This is easy to understand, but I want to pipe it's input/output by another python program. (I will show what I am doing on the console to test it)

>>> from subprocess import *
>>> p = Popen(["python", "increment.py"], stdin=PIPE, stdout=PIPE)
>>> p.communicate("5")
Traceback (most recent call last):
  File "increment.py", line 4, in <module>
    n = int(raw_input(n)) + 1
EOFError: EOF when reading a line
('06', None)
>>> p.communicate("7")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/subprocess.py", line 701, in communicate
    return self._communicate(input)
  File "/usr/lib/python2.6/subprocess.py", line 1184, in _communicate
    self.stdin.flush()
ValueError: I/O operation on closed file

The problem is that I want to use communicate multiple times before closing the file.

#2
Red_Shadow

Red_Shadow

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
See:
35.11. pipes ? Interface to shell pipelines — Python v2.6.5 documentation
"The only means of strengthening one's intellect is to make up one's mind about nothing - to let the mind be a thoroughfare for all thoughts." -- John Keats

#3
Somelauw

Somelauw

    Newbie

  • Members
  • PipPip
  • 18 posts

Red_Shadow said:


I am not using pipes-module, but I am using subprocess-module instead.
I don't want to use pipes-module since my file needs to be able to handle both input and output.

If it's still possible to read and write to a subprocess at the same time using the pipes-module, could you please post an example in which you use increment.py.

#4
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
Are you on Windows? I had this very annoying problem when I used subprocess, the pipe kept closing after one or two operations...and I never found a solution. :S

#5
Excited

Excited

    Newbie

  • Members
  • PipPip
  • 27 posts
local sockets

#6
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts

Excited said:

local sockets
What about other processes?
Like if I want to interface sql from python?
I can't beleive there is no way to communicate multiple times...