# Reconfigure example with lock/disconnect/connect/unlock code hangs randomly

**URL:** <https://www.ruby-forum.com/t/reconfigure-example-with-lock-disconnect-connect-unlock-code-hangs-randomly/207485>\
**Category:** GNU Radio\
**Created:** [May 26, 2011, 4:50pm UTC](https://www.ruby-forum.com/t/reconfigure-example-with-lock-disconnect-connect-unlock-code-hangs-randomly/207485 "2011-05-26T16:50:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [May 26, 2011, 4:50pm UTC](https://www.ruby-forum.com/t/reconfigure-example-with-lock-disconnect-connect-unlock-code-hangs-randomly/207485/1 "2011-05-26T16:50:23Z")

</div>

Hey guys,  
I have written some example code to learn how to dynamically  
reconfigure gnuradio based on events generated by probe blocks and a  
watcher thread.  
First I thought everything is running very well but later I noticed  
that the program hangs if you let it run long enough.  
If you uncomment the sleep(0.0001) it seems to be working fine but says:

“Exception in thread Thread-1 (most likely raised during interpreter  
shutdown)”

We are running Ubuntu 10.10 over here.

Can somebody please run the code and confirm this?  
Looks like a bug for me.

* * *

#!/usr/bin/env python

from gnuradio import gr  
import gnuradio.gr.gr\_threading as \_threading  
from time import sleep

class h\_block\_one(gr.hier\_block2):  
def **init** (self):  
gr.hier\_block2. **init** (self, “foo”,  
gr.io\_signature(0, 0, 0),  
gr.io\_signature(0, 0, 0))

```
    vec = range(100)

    self.source = gr.vector_source_f(vec,True,1)
    self.probe = gr.probe_signal_f()
    self.sink = gr.null_sink(gr.sizeof_float)
    self.connect(self.source, self.probe)

def level_one(self):
    return self.probe.level()

```

class h\_block\_two(gr.hier\_block2):  
def **init** (self):  
gr.hier\_block2. **init** (self, “bar”,  
gr.io\_signature(0, 0, 0),  
gr.io\_signature(0, 0, 0))

```
    vec = range(100)

    self.source = gr.vector_source_f(vec,True,1)
    self.probe = gr.probe_signal_f()
    self.sink = gr.null_sink(gr.sizeof_float)
    self.connect(self.source, self.probe)

def level_two(self):
    return self.probe.level()

```

class probe\_this(gr.top\_block):

```
def __init__ (self):
    gr.top_block. __init__ (self, 'Probe Reconfigure Test')

    self.h_block_one = h_block_one()
    self.h_block_two = h_block_two()
    self.connect(self.h_block_one)

def level_one(self):
    return self.h_block_one.level_one()

def level_two(self):
    return self.h_block_two.level_two()

def reconfigure(self):
    self.lock()
    self.disconnect(self.h_block_one)
    self.connect(self.h_block_two)
    self.unlock()

def conf_back(self):
    self.lock()
    self.disconnect(self.h_block_two)
    self.connect(self.h_block_one)

```

# sleep(0.0001)

```
    self.unlock()

```

class \_probe\_watcher\_thread(\_threading.Thread):  
def **init** (self, level\_one, reconfigure, level\_two, conf\_back):  
\_threading.Thread. **init** (self)  
self.setDaemon(1)  
self.level\_one = level\_one  
self.reconfigure = reconfigure  
self.level\_two = level\_two  
self.conf\_back = conf\_back  
self.confed = False  
self.keep\_running = True  
self.start()

```
def run(self):
    while self.keep_running:
        if self.confed:
            if self.level_two() > 50:
                self.conf_back()
                self.confed = False
                print 'reconfiguring'
        else:
            if self.level_one() > 50:
                self.reconfigure()
                self.confed = True
                print 'reconfiguring back'

```

# if self.level\_one and self.level\_two:

# print ‘foo’, self.level\_one()

# print ‘bar’, self.level\_two()

if **name** ==“ **main** ”:

```
def callback(msg):
    print 'received:', msg

tb = probe_this()
watcher = 

```

\_probe\_watcher\_thread(tb.level\_one,tb.reconfigure,tb.level\_two,tb.conf\_back)  
tb.start()

```
sleep(100)
```

---

<div class="post-metadata">

**Author:** ![jst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jst](https://www.ruby-forum.com/u/jst)\
**Post date:** [May 26, 2011, 4:57pm UTC](https://www.ruby-forum.com/t/reconfigure-example-with-lock-disconnect-connect-unlock-code-hangs-randomly/207485/2 "2011-05-26T16:57:48Z")

</div>

There should be another sleep in the reconfigure method to make it  
working fine.

def reconfigure(self):  
self.lock()  
self.disconnect(self.h\_block\_one)  
self.connect(self.h\_block\_two)  
sleep(0.0001)  
self.unlock()

I am not sure how long we have to sleep in order to fix the problem.

- Johannes
