我正在按照此线程中的示例尝试将消息发布到远程 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()