Simulate `bash` behaviour using Python and named pipes.

Alister alister.ware at ntlworld.com
Mon Aug 5 10:29:51 EDT 2013


On Mon, 05 Aug 2013 06:09:53 -0700, Luca Cerone wrote:

> Hi everybody,
> I am trying to understand how to use named pipes in python to launch
> external processes (in a Linux environment).
> 
> As an example I am trying to "imitate" the behaviour of the following
> sets of commands is bash:
> 
>> mkfifo named_pipe ls -lah > named_pipe &
>> cat < named_pipe
> 
> In Python I have tried the following commands:
> 
> import os import subprocess as sp
> 
> os.mkfifo("named_pipe",0777) #equivalent to mkfifo in bash..
> fw = open("named_pipe",'w')
> #at this point the system hangs...
> 
> My idea it was to use subprocess.Popen and redirect stdout to fw...
> next open named_pipe for reading and giving it as input to cat (still
> using Popen).
> 
> I know it is a simple (and rather stupid) example, but I can't manage to
> make it work..
> 
> 
> How would you implement such simple scenario?
> 
> Thanks a lot in advance for the help!!!
> 
> Luca

Are you sure you are using the correct tool for the task?

I tend to find that in most cases if you are trying to execute bash 
commands from Python you are doing it wrong.

certainly if you are trying to pipe one bash command into another you 
would probably be better of with a bash script.




-- 
When I was crossing the border into Canada, they asked if
I had any firearms with me.  I said, "Well, what do you need?"
		-- Steven Wright



More information about the Python-list mailing list