VIP publish function not timing out when remote platform is dead

我是研究僧i 提交于 2019-12-12 00:38:48

问题


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:


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.



来源:https://stackoverflow.com/questions/41427721/vip-publish-function-not-timing-out-when-remote-platform-is-dead

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!