top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Simulate `bash` behaviour using Python and named pipes.

+1 vote
1,021 views

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?

posted Aug 5, 2013 by Sonu Jindal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

You can pipe using subprocess
p1 = subprocess.Popen(["ls", "-lah"], stdout=subprocess.PIPE) p2 = subprocess.Popen(["cat"], stdin=p1.stdout)p1.wait()p2.wait()

You can also pass a file object to p1s stdout and p2s stdin if you want to pipe via a file. with open("named_pipe", "rw") as named_pipe:
p1 = subprocess.Popen(["ls", "-lah"], stdout=named_pipe) p2 = subprocess.Popen(["cat"], stdin=named_pipe) p1.wait() p2.wait() Luca

answer Aug 5, 2013 by Luv Kumar
I am aware of the first solutions, just now I would like to experiment a bit with using named pipes (I also know that the example is trivial, but it just to grasp the main concepts)

Your second example doesn't work for me.. if named_file is not a file in the folder I get an error saying that there is not such a file.

If I create named_pipe as a named pipe using os.mkfifo("named_file",0777) than the code hangs.. I think it is because there is not process that reads the content of the pipe, so the system waits for the pipe to be emptied.
Similar Questions
0 votes

Have the following in xfce autostart:

#!/bin/bash
# Disable screensaver and dpms emergy star.
export DISPLAY :0.0 
xset s off && 
xset -dpms 

on running:

./xset
./xset: line 4: export: `:0.0': not a valid identifier

It appears to be the correct display going by:

echo $DISPLAY
:0.0
0 votes

Here, I want to filter some specific files and directories.

+1 vote

I have got a bash script that has two parts.
1. runs a script as a different user (using su -c )
2. when that part finishes, the script does a copy of the files created to another directory
I want to log the output of the entire script into one file. i.e.: internal script>external script> logfile
Whats the best way to do this?

+2 votes
win32api.keybd_event(code,0,0,0)
time.sleep(2)
win32api.keybd_event(code,0,win32con.KEYEVENTF_KEYUP,0)

Above code is simulating single click on button but not press Key Hold but I want to hold the until key up event is called
eg: for key 'a' down it have to simulate key continous set until keyUp is called but not like "a"

Please specify any python API to simulate continues keyDown until it receives keyUp event

...