2012-07-11 14 views
1

我已經繪製出了一個流程圖,我想遵循,但無法弄清楚如何執行此操作。目前我有以下幾點。重新啓動Python中的線程並檢查異常

t1是一個數據庫插入腳本,不是cherrypy。

def main(): 
    thread = ThreadUrl(queue) 
    thread = thread() 
    thread.start() 
    cherrypy.config.update({'server.socket_host': '0.0.0.0', 
          'server.socket_port': 2970}) 
          #'server.thread_pool': 100}) 
    queue.join() 
    cherrypy.engine.start() 
    while True: 
     if thread.isAlive(): 
      try: 
       cherrypy.engine.start() 
      except Exception: 
       print ('Started cherrypy already.') 
      print ('I am alive.') 
     else: 
      try: 
       thread.exit() 
      except Exception: 
       print ('Already killed this thread.') 
      print ('I am dead.') 
      try: 
       cherrypy.engine.stop() 
      except Exception: 
       print ('Already stopped cherrypy.') 
      try: 
       thread.start() 
      except Exception: 
       print (sys.exc_info()[1]) 

if __name__ == '__main__': 
    main() 

I would like to follow this flow chart

+0

這似乎......有點令人費解,因爲'cherrypy.engine'已經實現了自己的狀態機,並且只有在調用'cherrypy.engine.block()'時纔會阻塞調用線程。你想用包裝線完成什麼? – fumanchu 2012-07-11 19:55:01

+0

嗨,對不起,我忘了在描述中添加一行有點重要,t1是一個數據庫插入腳本不cherrypy。 – 2012-07-11 21:20:44

回答

0

你可以在一個字典創建一個狀態表。每個節點將在執行其行動的功能,然後返回下一個狀態,這樣的事情:

def start_t1(): 
    # Start t1. 
    return "start cherrypy" 

def start_cherrypy(): 
    # Start Cherrypy. 
    return "is t1 alive?" 

def is_cherrypy_alive(): 
    # Check whether Cherrypy is alive. 
    if alive: 
     return "stop cherrypy" 
    return "start t1" 

actions = { 
    "start t1": start_t1, 
    "start cherrypy": start_cherrypy, 
    "is t1 alive?": is_t1_alive, 
    "is cherry pie alive?": is_cherrypy_alive, 
    ... 
} 

state = "start t1" 
while True: 
    state = actions[state]() 
+0

嗨,對不起,我忘了在描述中添加一行有點重要,t1是一個數據庫插入腳本不cherrypy。 – 2012-07-11 21:20:52