2016-09-28 19 views
-1

我在運行我的C++代碼庫時退出了我的python代碼。我似乎無法弄清楚,如果它的Python代碼做它或我的C++。任何幫助將不勝感激。使用python封裝時C++代碼不能退出

這裏是我的CPP

class Led { 
    public: 
    void snowled() 
    { 
     LinuxGPIO gpio23(23); 
     gpio23.SetDirection(true); 
     bool on = true; 
     for (;;) 
     { 
      printf("Switching %s the LED...\n", on ? "on" : "off"); 
      gpio23.SetValue(on); 
      on = !on; 
      sleep(1); 
     } 
    } 
}; 



extern "C" { 
    Led* Led_new(){ return new Led(); } 
    void Led_snowled(Led* led){ led->snowled(); } 
} 

這裏是我的Python I路打過電話sys.exit(),但它不會退出。

import sys 
import time 
from ctypes import cdll 
lib = cdll.LoadLibrary('./snowled.so') 

class Led(object): 

    def __init__(self): 
     self.obj = lib.Led_new() 

    def snowled(self): 
     lib.Led_snowled(self.obj) 

    def sleeper(self): 
     num = float(1) 
     time.sleep(num) 

    def main(self): 
     light = Led() 
     light.snowled() 

    def stop_running(self): 
     try: 
     sys.exit() 
     except: 
     print(sys.exc_info()[0]) 

if __name__=='__main__': 
    Led().main() 
    Led().stop_running() 
+3

考慮到你有一個infite循環,我不確定你的實際問題是什麼。 –

+0

請在您的代碼中添加記錄註釋。你的Python腳本似乎沒有做你說你想要做的事情。 –

回答

0

呼叫sys.exit()實際上提出一個SystemExit例外。沒有參數,退出狀態爲零。

你爲什麼抓住它?