Pair of filenos read/write each other?

J

Jack Bates

Can anyone suggest a way to get a pair of file descriptor numbers such
that data written to one can be read from the other and vice versa?

Is there anything like os.pipe() where you can read/write both ends?

Thanks!
 
R

Roy Smith

Jack Bates said:
Can anyone suggest a way to get a pair of file descriptor numbers such
that data written to one can be read from the other and vice versa?

Is there anything like os.pipe() where you can read/write both ends?

Thanks!

I think you're looking for socket.socketpair()
 
N

Nobody

Is there anything like os.pipe() where you can read/write both ends?

There's socket.socketpair(), but it's only available on Unix.

Windows doesn't have AF_UNIX sockets, and anonymous pipes (like the ones
created by os.pipe()) aren't bidirectional. Named pipes are bidirectional,
but you would need to choose a name, create one, connect, and accept (like
with sockets); there's no convenience function like socketpair(). Also,
you need to consider the security implications, as other processes can
(try to) connect to a named pipe.
 
A

Antoine Pitrou

Nobody said:
There's socket.socketpair(), but it's only available on Unix.

Windows doesn't have AF_UNIX sockets, and anonymous pipes (like the ones
created by os.pipe()) aren't bidirectional.

I'm not sure I understand the problem: you can just create two pair of pipes
using os.pipe().
If that's too low-level, you can wrap the fds using BufferedRWPair:
http://docs.python.org/3.3/library/io.html#io.BufferedRWPair

(actual incantation would be:
r1, w1 = os.pipe()
r2, w2 = os.pipe()

end1 = io.BufferedRWPair(io.FileIO(r1, 'r'), io.FileIO(w2, 'w'))
end2 = io.BufferedRWPair(io.FileIO(r2, 'r'), io.FileIO(w1, 'w'))

end1.write(b"foo")
end1.flush()
end2.read(3) # -> return b"foo"
)

An alternative is to use multiprocessing.Pipe():
http://docs.python.org/3.3/library/multiprocessing.html#multiprocessing.Pipe

In any case, Python doesn't lack facilities for doing what you want.

Regards

Antoine.
 
J

Jack Bates

I'm not sure I understand the problem: you can just create two pair of pipes
using os.pipe().
If that's too low-level, you can wrap the fds using BufferedRWPair:
http://docs.python.org/3.3/library/io.html#io.BufferedRWPair

(actual incantation would be:
r1, w1 = os.pipe()
r2, w2 = os.pipe()

end1 = io.BufferedRWPair(io.FileIO(r1, 'r'), io.FileIO(w2, 'w'))
end2 = io.BufferedRWPair(io.FileIO(r2, 'r'), io.FileIO(w1, 'w'))

end1.write(b"foo")
end1.flush()
end2.read(3) # -> return b"foo"
)

An alternative is to use multiprocessing.Pipe():
http://docs.python.org/3.3/library/multiprocessing.html#multiprocessing.Pipe

In any case, Python doesn't lack facilities for doing what you want.

Thank you for your help, I need to satisfy an interface that requires a single
file descriptor number that can be both read from and written to. Is it
possible with any of the solutions you pointed out to get a single file
descriptor number for each end?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top