Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52503

Re: Pair of filenos read/write each other?

From Antoine Pitrou <solipsis@pitrou.net>
Subject Re: Pair of filenos read/write each other?
Date 2013-08-14 08:34 +0000
References <mailman.552.1376435986.1251.python-list@python.org> <pan.2013.08.14.07.21.36.218000@nowhere.com>
Newsgroups comp.lang.python
Message-ID <mailman.566.1376469298.1251.python-list@python.org> (permalink)

Show all headers | View raw


Nobody <nobody <at> nowhere.com> writes:
> 
> On Tue, 13 Aug 2013 16:10:41 -0700, Jack Bates wrote:
> 
> > 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.

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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Pair of filenos read/write each other? Jack Bates <tdhfwh@nottheoilrig.com> - 2013-08-13 16:10 -0700
  Re: Pair of filenos read/write each other? "Rhodri James" <rhodri@wildebst.demon.co.uk> - 2013-08-14 01:17 +0100
    Re: Pair of filenos read/write each other? Chris Angelico <rosuav@gmail.com> - 2013-08-14 01:55 +0100
    Re: Pair of filenos read/write each other? Jack Bates <tdhfwh@nottheoilrig.com> - 2013-08-15 08:56 -0700
    Re: Pair of filenos read/write each other? Jack Bates <tdhfwh@nottheoilrig.com> - 2013-08-15 08:59 -0700
  Re: Pair of filenos read/write each other? Roy Smith <roy@panix.com> - 2013-08-13 21:37 -0400
  Re: Pair of filenos read/write each other? Nobody <nobody@nowhere.com> - 2013-08-14 08:21 +0100
    Re: Pair of filenos read/write each other? Antoine Pitrou <solipsis@pitrou.net> - 2013-08-14 08:34 +0000
    Re: Pair of filenos read/write each other? Jack Bates <tdhfwh@nottheoilrig.com> - 2013-08-15 09:19 -0700
    Re: Pair of filenos read/write each other? Antoine Pitrou <solipsis@pitrou.net> - 2013-08-15 18:24 +0000

csiph-web