首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当远程平台关闭时,VIP发布功能不超时

当远程平台关闭时,VIP发布功能不超时
EN

Stack Overflow用户
提问于 2017-01-02 13:23:50
回答 1查看 100关注 0票数 0

我正在遵循this thread中的示例,尝试将消息发布到远程VOLTTRON平台,当远程平台正确运行和设置时,它运行良好。但是,当远程平台没有运行时,发布函数将永远处于阻塞状态,不会超时。这样可以防止检测远程平台没有运行时的情况,还会阻止代码其余部分的执行。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2017-01-31 21:40:59

作为对vip.pubsub.publish method does not timeout的回应,这也是一个问题。

此代理的代码不正确。my_event.wait()不抛出异常,它返回一个真值或假值。

因此,代码应该有如下内容:

代码语言:javascript
复制
if not my_event.wait(timeout=5):
    print('Bad thing here')
    sys.exit()

或者你可以用

代码语言:javascript
复制
with gevent.Timeout(timeout=5):
    event.wait()  # note not gevent

请参阅https://github.com/VOLTTRON/volttron/blob/develop/volttron/platform/vip/agent/utils.py以了解我们如何处理这一问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41427721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档