I started writing a plugin for Blender that accepts Open Sound Control messages for direct manipulation of data in realtime. I ran into a snag that made me uneasy about releasing it: when I close Blender, it crashes because the thread is still open and running. So I have to either force quit Blender or Ctrl-C from the command line to kill it.
How do I correctly catch Blender closing so I can cleanly run the command to stop the process running on another thread?
Here's a sample of the code:
def register():
bpy.utils.register_class(OSCPanel)
disp = dispatcher.Dispatcher()
disp.map("/*", oscHandler)
BpyOSCServer = osc_server.ThreadingOSCUDPServer(("0.0.0.0", 60004), disp)
server_thread = threading.Thread(target=BpyOSCServer.serve_forever)
server_thread.start()
print('BpyOSC registered!')
def unregister():
bpy.utils.unregister_class(OSCPanel)
print('Stopping server')
BpyOSCServer.shutdown()
print('BpyOSC unregistered!')