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


Groups > comp.lang.python > #52480 > unrolled thread

Pair of filenos read/write each other?

Started byJack Bates <tdhfwh@nottheoilrig.com>
First post2013-08-13 16:10 -0700
Last post2013-08-15 18:24 +0000
Articles 10 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  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

#52480 — Pair of filenos read/write each other?

FromJack Bates <tdhfwh@nottheoilrig.com>
Date2013-08-13 16:10 -0700
SubjectPair of filenos read/write each other?
Message-ID<mailman.552.1376435986.1251.python-list@python.org>
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!

[toc] | [next] | [standalone]


#52484

From"Rhodri James" <rhodri@wildebst.demon.co.uk>
Date2013-08-14 01:17 +0100
Message-ID<op.w1r5rqzha8ncjz@gnudebeest>
In reply to#52480
On Wed, 14 Aug 2013 00:10:41 +0100, Jack Bates <tdhfwh@nottheoilrig.com>  
wrote:

> 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?

Sockets?  It depends a bit on what you're trying to do, exactly.  If you  
give us a bit more context, we might be able to give you better advice.

-- 
Rhodri James *-* Wildebeest Herder to the Masses

[toc] | [prev] | [next] | [standalone]


#52485

FromChris Angelico <rosuav@gmail.com>
Date2013-08-14 01:55 +0100
Message-ID<mailman.556.1376441747.1251.python-list@python.org>
In reply to#52484
On Wed, Aug 14, 2013 at 1:17 AM, Rhodri James
<rhodri@wildebst.demon.co.uk> wrote:
> On Wed, 14 Aug 2013 00:10:41 +0100, Jack Bates <tdhfwh@nottheoilrig.com>
> wrote:
>
>> 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?
>
>
> Sockets?  It depends a bit on what you're trying to do, exactly.  If you
> give us a bit more context, we might be able to give you better advice.

Specific questions:

1) Do you need different processes to do the reading/writing?
2) Do you need to separate individual writes (message mode)?

ChrisA

[toc] | [prev] | [next] | [standalone]


#52555

FromJack Bates <tdhfwh@nottheoilrig.com>
Date2013-08-15 08:56 -0700
Message-ID<mailman.600.1376582179.1251.python-list@python.org>
In reply to#52484
On Wed, Aug 14, 2013 at 01:17:40AM +0100, Rhodri James wrote:
> On Wed, 14 Aug 2013 00:10:41 +0100, Jack Bates <tdhfwh@nottheoilrig.com>  
> wrote:
> 
> > 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?
> 
> Sockets?  It depends a bit on what you're trying to do, exactly.  If you  
> give us a bit more context, we might be able to give you better advice.

Thanks, I am writing a fixture to test an app. The app normally uses the Python
wrapper for the GnuTLS library to handshake with a service that I want to
mock up in my fixture. The app passes a file descriptor number to
gnutls_transport_set_ptr()

When I test the app, the app and fixture run in the same process. I want a file
descriptor number I can foist on the app, and in the fixture access what is
read/written to it by the GnuTLS wrapper.

[toc] | [prev] | [next] | [standalone]


#52556

FromJack Bates <tdhfwh@nottheoilrig.com>
Date2013-08-15 08:59 -0700
Message-ID<mailman.601.1376582372.1251.python-list@python.org>
In reply to#52484
On Wed, Aug 14, 2013 at 01:55:38AM +0100, Chris Angelico wrote:
> On Wed, Aug 14, 2013 at 1:17 AM, Rhodri James
> <rhodri@wildebst.demon.co.uk> wrote:
> > On Wed, 14 Aug 2013 00:10:41 +0100, Jack Bates <tdhfwh@nottheoilrig.com>
> > wrote:
> >
> >> 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?
> >
> >
> > Sockets?  It depends a bit on what you're trying to do, exactly.  If you
> > give us a bit more context, we might be able to give you better advice.
> 
> Specific questions:
> 
> 1) Do you need different processes to do the reading/writing?

No, only one process is involved.

> 2) Do you need to separate individual writes (message mode)?

No, data can be split into smaller writes or merged into a bigger write, so
long as it arrives in order.

> ChrisA

Thank you for your help!

[toc] | [prev] | [next] | [standalone]


#52488

FromRoy Smith <roy@panix.com>
Date2013-08-13 21:37 -0400
Message-ID<roy-A2759C.21375513082013@news.panix.com>
In reply to#52480
In article <mailman.552.1376435986.1251.python-list@python.org>,
 Jack Bates <tdhfwh@nottheoilrig.com> wrote:

> 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()

[toc] | [prev] | [next] | [standalone]


#52501

FromNobody <nobody@nowhere.com>
Date2013-08-14 08:21 +0100
Message-ID<pan.2013.08.14.07.21.36.218000@nowhere.com>
In reply to#52480
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. 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.

[toc] | [prev] | [next] | [standalone]


#52503

FromAntoine Pitrou <solipsis@pitrou.net>
Date2013-08-14 08:34 +0000
Message-ID<mailman.566.1376469298.1251.python-list@python.org>
In reply to#52501
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.

[toc] | [prev] | [next] | [standalone]


#52557

FromJack Bates <tdhfwh@nottheoilrig.com>
Date2013-08-15 09:19 -0700
Message-ID<mailman.602.1376583595.1251.python-list@python.org>
In reply to#52501
On Wed, Aug 14, 2013 at 08:34:36AM +0000, Antoine Pitrou wrote:
> 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.

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?

[toc] | [prev] | [next] | [standalone]


#52564

FromAntoine Pitrou <solipsis@pitrou.net>
Date2013-08-15 18:24 +0000
Message-ID<mailman.606.1376591064.1251.python-list@python.org>
In reply to#52501
Jack Bates <tdhfwh <at> nottheoilrig.com> writes:
> > 
> > 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?

Yes, it is what multiprocessing.Pipe() provides:
http://docs.python.org/3.3/library/multiprocessing.html#multiprocessing.Connection.fileno

Regards

Antoine.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web