What is the best way to read a named pipe continuously in Python?
This is my current code:
def read_commands():
try:
print "Creating read pipe..."
os.mkfifo(pipe_cmd) # Create pipe
print "Pipe created!"
except:
print "Pipe already exists"
with open(pipe_cmd, "r") as pipecmd:
while True:
try:
line = pipecmd.readline()
except:
print "Could not read cmd pipe"
if line != "":
print line
#time.sleep(1)
However, when I run this code it seems to be taking a lot of resources from my CPU (One of them will go to 100%). It works fine with the sleep of 1 second. However, I need to read the pipe continuously to make sure if there is new data . Is there a better way to accomplish this?
This is what I am sending to the pipe in C++:
void write_pipe(){
ofstream pipe("/tmp/okccmd"); // Open the pipe
string data = "Hi";
pipe << data << endl;
pipe.flush();
}
Thanks!
select.poll works fine (at least for Linux, not sure if Windows supports this; select.select ist afaik available, however). Just have a look at the documentation, the module is in the standard library and well documented (there is no need to know how the OS select() function actually works).
Documentation: https://docs.python.org/3/library/select.html
Note: poll() returns a list of file descriptors, not file-objects. So, you should have a dict which maps file descriptors to the corresponding object (I would have this also if I just poll one file.
pollobj = select.poll()
polled_files = dict()
# the following two lines are reuired for every file
pollobj.register(my_file_obj, <EVENTMASK>)
polled_files[my_file_obj.fileno()] = my_file_obj
for fd, evt in pollobj.poll():
fileobj = polled_files[fd]
... process event for fileobj
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.