用Python包装时不退出的C++代码

2024-10-01 07:37:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我在运行c++代码库时遇到python代码退出的问题。我似乎不知道是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 #我尝试调用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()

Tags: 代码importselftruenewledmainon