VIP publish function not timing out when remote platform is dead

104 views Asked by At

I am following the example in this thread to try to publish messages to a remote VOLTTRON platform, and it is working fine when the remote platform is running and set-up correctly. However, when the remote platform is not running, the publish function remains blocking forever and won't time out. This prevents detection of when the remote platform is not running, and also prevents execution of rest of the code.

from volttron.platform.vip.agent import Core, Agent
import gevent

def vip_publish(topic,message, address=None):
    retry = 3
    while retry>0:
        pub_agent = Agent(address=address)
        my_event = gevent.event.Event()
        pub_agent.core.onstart.connect(lambda *a, **kw: my_event.set(),my_event)
        agent_thread = gevent.spawn(pub_agent.core.run)
        my_event.wait()
        try:
            #The following line remains blocking forever when remote volttron platform is not running
            pub_agent.vip.pubsub.publish(peer='pubsub', topic=topic, message=message).get(timeout=1)
        except gevent.Timeout:
            print "Time-out"
            retry -= 1
        else:
            retry = 0
        agent_thread.kill()
1

There are 1 answers

0
Craig On

In response to vip.pubsub.publish method does not timeout as well this question.

The code for this agent isn't correct. my_event.wait() does not throw an exception it returns a true or false value.

So instead the code should have something like:

if not my_event.wait(timeout=5):
    print('Bad thing here')
    sys.exit()

or you can use

with gevent.Timeout(timeout=5):
    event.wait()  # note not gevent

See https://github.com/VOLTTRON/volttron/blob/develop/volttron/platform/vip/agent/utils.py to see how we deal with this.