Forum: GNU Radio Passing file descriptors via SWIG

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B22e1b01ee2edc1bdd77eeb90fd4d5b4?d=identicon&s=25 Martin Braun (Guest)
on 2009-03-13 13:50
(Received via mailing list)
Hi,

following
http://lists.gnu.org/archive/html/discuss-gnuradio...,
I tried connecting two processes with a FIFO using gr.file_descriptor_*.

However, I can't seem to pass the file descriptor from the Python to the
C++ domain. This is the error message I get:

gr_file_descriptor_sink: Bad file descriptor

This is the code (pipe1 was created by using 'mkfifo pipe1'):

Source-File:

<<< SNIP >>>
from gnuradio import gr
class sig_source(gr.top_block):
    def __init__(self):
        gr.top_block.__init__(self)

        src = gr.sig_source_f(8000, gr.GR_SIN_WAVE, 2000, 1.0)
        head = gr.head(gr.sizeof_float, 8000)

        print "Opening pipe..."
        fid = open('pipe1', 'wb')
        print "Done. fileno: ", fid.fileno()
        sink = gr.file_descriptor_sink(gr.sizeof_float, fid.fileno())

        self.connect(src, head, sink)

if __name__ == '__main__':
    try:
        sig_source().run()
    except KeyboardInterrupt:
        pass
<<< SNIP >>>

Sink:
<<< SNIP >>>
from gnuradio import gr
class sig_sink(gr.top_block):
    def __init__(self):
        gr.top_block.__init__(self)

        print "Opening pipe..."
        fid = open('pipe1', 'rb')
        print "Done. fileno: ", fid.fileno()
        src = gr.file_descriptor_source(gr.sizeof_float, fid.fileno())
        self.sink = gr.vector_sink_f()

        self.connect(src, self.sink)

if __name__ == '__main__':
    try:
        sigsnk = sig_sink()
        sigsnk.run()
        print sigsnk.sink.data()[0:4]
    except KeyboardInterrupt:
        pass
<<< SNIP >>>

When I start both scripts, this is what I get:

Opening pipe...
Done. fileno:  3
file_descriptor_source[read]: Bad file descriptor

on both the write and read ends of the pipe. This does work when I open
the pipe with gr_file_sink, but then I have to think about the
buffering.

Has anyone ever done this and could drop me some example code that
works? I'd really appreciate it...

Cheers
MB
745d8202ef5a58c1058d0e5395a78f9c?d=identicon&s=25 Eric Blossom (Guest)
on 2009-03-13 15:11
(Received via mailing list)
On Fri, Mar 13, 2009 at 01:49:31PM +0100, Martin Braun wrote:
>
> This is the code (pipe1 was created by using 'mkfifo pipe1'):

The python file object, fid, is going out of scope, and thus the
underlying file is being closed.  Try self.fid = open(...)

Eric
B22e1b01ee2edc1bdd77eeb90fd4d5b4?d=identicon&s=25 Martin Braun (Guest)
on 2009-03-13 16:39
(Received via mailing list)
On Fri, Mar 13, 2009 at 07:09:26AM -0700, Eric Blossom wrote:
> > gr_file_descriptor_sink: Bad file descriptor
> >
> > This is the code (pipe1 was created by using 'mkfifo pipe1'):
>
> The python file object, fid, is going out of scope, and thus the
> underlying file is being closed.  Try self.fid = open(...)

Thanks, Eric, that helped.
If anyone ever tries doing this: Both Python and gr_file_descriptor_sink
will try to close the file, resulting in a warning. If you ask me,
gr_file_descriptor_* should not close the file in the destructors - if
anyone's using these blocks, they should usually know what they're
doing. Plus, to use a gr_file_descriptor, you need to open the file
yourself beforehand, the closing should thus happen in the same scope. I
suggest kicking the close() lines from
gr_file_descriptor*::~gr_file_descriptor*.

Just my two Euro-cents...
MB
This topic is locked and can not be replied to.