2017-01-02 20 views
0

我下面在this thread例子來嘗試發佈消息發送到遠程VOLTTRON平臺,並在遠程平臺運行和設置正確它工作正常。 但是,當遠程平臺未運行時,發佈功能將永遠保持阻止狀態,並且不會超時。這可以防止檢測遠程平臺何時未運行,並防止執行其餘代碼。VIP發佈功能不超時當遠程平臺是死

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() 

回答

0

迴應vip.pubsub.publish method does not timeout以及這個問題。

此代理的代碼是不正確的。 my_event.wait()不會引發異常,它會返回true或false值。

所以不是代碼應該是這樣:

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,看看我們如何處理這個問題。