write_to_named_pipe.py
import os if __name__ == "__main__": named_pipe = "my_pipe" if not os.path.exists(named_pipe): os.mkfifo(named_pipe) def write_message(input_pipe, message): fd = os.open(input_pipe, os.O_WRONLY) os.write(fd, (message + str(os.getpid()))) os.close(fd) write_message(named_pipe, "from write_pipe...")
read_from_named_pipe.py
import os if __name__ == "__main__": named_pipe = "my_pipe" def read_message(input_pipe): fd = os.open(input_pipe, os.O_RDONLY) message = ("I pid [%d] received a message => %s" %(os.getpid(), os.read(fd, 22))) os.close(fd) return message print read_message(named_pipe)