0

I've got a process that reads console input from user (readline, raw_input, etc. - depending on platform). There is another process that wants to feed the first process with a given input.

How to do this in linux? Is it possible? PS the language I'm interested is Python, but hints on any language/platform are appreciated.

ducin
  • 25,621
  • 41
  • 157
  • 256

2 Answers2

2

The first option:

Python provides subprocess package to perform this task. You need to use pipes provided with subprocess package. Reference here.

The second option:

You can use multiprocessing package for better control and more options; pipes still available. Additionally you can use Queue, Array and Lock to facilitate interprocess communication. Reference here.

sgun
  • 899
  • 6
  • 12
  • the first option (subprocess/pipe) is the answer for me. Thanks a lot! – ducin Aug 04 '13 at 18:37
  • Try to use Python 2.x, python 3.x has some platform specific (UNIX) subprocess functions which will give you trouble on windows. I am glad it helped. – sgun Aug 04 '13 at 18:39
  • The lines I needed are: `import sys`/`sys.stdin = open('file', 'r')` – ducin Aug 04 '13 at 18:43
1

In general pipes work well on unix-like systems. In C you would call popen which returns both ends of the pipe and then fork to produce a process that writes to the pipe and one that reads from it. The code is standard boilerplate:

int pdes[2];

pipe(pdes);
if ( fork() == 0 ) { 
    close(pdes[1]); 
    read( pdes[0]); /* read from parent */
    .....
}
else {          
         close(pdes[0]); 
     write( pdes[1]); /* write to child */
         .....
}

In Python you can use subprocesses and communicate using pipes as described in the python documentation

David Elliman
  • 1,379
  • 8
  • 15