0

In the following GNU Radio processing block, I can't figure out who/what passes the value of input_items to the work function in the first place. Is it possible to pass that value to the __init__ function instead?

I have a file xyz.py :

class xyz(gr.sync_block):
    """
    docstring for block add_python
    """
    def __init__(self, parent, title, order):
        gr.sync_block.__init__(self,
            name="xyz",
            in_sig=[numpy.float32,numpy.float32],
            out_sig=None)
            ................
           ................
           //I want to access the value of input_items here
           ...............
           ...............


    def work(self, input_items, output_items):
        ................
djanderson
  • 563
  • 4
  • 13
Karup
  • 2,024
  • 3
  • 22
  • 48

2 Answers2

4

The __init__ function is called only once to "initialize" the new instance of the class. It has nothing to do with moving data through the flowgraph, besides setting up input and output types so that the block can be successfully connected.

So, in top_block, you might have:

proc = xyz() # xyz's __init__ is called here
self.connect(source, proc, sink) # still no input_items, just connected flowgraph

And later on, you run the flowgraph:

tb = top_block()
tb.run() # 'input_items' are now passed to 'work' of each block in succession

When you run the flowgraph, the GNU Radio scheduler takes a number of samples from the source block and puts them in a buffer. It then passes that buffer to the work function of the next block in the flowgraph, along with an "empty" buffer for output items.

So the work function of each block is called automatically by the scheduler when there is data for it to process. __init__ can not have access to any of the parameters to work, because input_items has not even been passed to work at the time that __init__ is called.

djanderson
  • 563
  • 4
  • 13
  • So there is absolutely no way I can access data in the `__init__` function of a sync_block? And how is it possible to access data in a **hier_block** (https://github.com/a-w-s/GNURadio-SBHS-Python/blob/master/python/plot_sink.py) `__init__` then? I wanted to add a panel on the top block frame which can only be done by assigning a panel to `self.win ` in `__init__` (like in the hier_block example). If I try to assign a panel to `self.win` inside the `work` function then it gives error : _'xyz' object has no attribute 'win'_ – Karup Jun 06 '15 at 19:11
  • No, there is absolutely no way to access `input_items` or `output_items` when the block is being initialized, for the reason I mentioned above. `hier_block` behaves quite differently than the sync block you asked about in this question. It would be best to create a separate question for that. – djanderson Jun 07 '15 at 20:36
  • http://stackoverflow.com/questions/30703138/how-exactly-is-hier-block-s-behaviour-different-then-that-of-sync-block-in-gnu – Karup Jun 08 '15 at 07:14
0

work is a function that is entirely seperate from __init__. Its parameters cannot be accessed outside of that function. If you want to access input_items, add it to the __init__ parameter list and pass it when you call __init__.

Ryan Goldstein
  • 523
  • 3
  • 14
  • In gnuradio there is an xml file for each block. Parameters are passed to the __init__ function from the xml file. I am unaware from where the parameters are passed to the work function. I think I have oversimplified the question here. Anyone with proper understanding of the synchronous block in gnuradio might be able to help – Karup Jun 04 '15 at 09:47