我正在遵循this thread中的示例,尝试将消息发布到远程VOLTTRON平台,当远程平台正确运行和设置时,它运行良好。但是,当远程平台没有运行时,发布函数将永远处于阻塞状态,不会超时。这样可以防止检测远程平台没有运行时的情况,还会阻止代码其余部分的执行。
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()发布于 2017-01-31 21:40:59
作为对vip.pubsub.publish method does not timeout的回应,这也是一个问题。
此代理的代码不正确。my_event.wait()不抛出异常,它返回一个真值或假值。
因此,代码应该有如下内容:
if not my_event.wait(timeout=5):
print('Bad thing here')
sys.exit()或者你可以用
with gevent.Timeout(timeout=5):
event.wait() # note not gevent请参阅https://github.com/VOLTTRON/volttron/blob/develop/volttron/platform/vip/agent/utils.py以了解我们如何处理这一问题。
https://stackoverflow.com/questions/41427721
复制相似问题