3
$\begingroup$

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!')
$\endgroup$
8
  • $\begingroup$ I would try and share some code so others can see how you're doing things. $\endgroup$ May 18, 2016 at 14:27
  • $\begingroup$ Maybe kill the thread using the methods outlined here blender.stackexchange.com/questions/39768/… $\endgroup$
    – batFINGER
    May 18, 2016 at 15:57
  • 1
    $\begingroup$ @batFINGER Oh, maybe I misunderstood that register an unregister execute when you start and quit blender respectively. $\endgroup$ May 19, 2016 at 13:49
  • 1
    $\begingroup$ Oh, just noticed your code, register and unregister methods are used to make the addon and its classes known to blender, and to clean up when unregistered. IMO not the place to put in server code. Are you familiar with the update callback on a say BoolProperty? giving an easy way to make a simple button to START server when True, and STOP when False? $\endgroup$
    – batFINGER
    May 19, 2016 at 14:08
  • 2
    $\begingroup$ I'm voting to close this question as off-topic because this is a programming question about threading, and it's not specific to Blender. This question is an EXCELLENT candidate for migration to StackOverflow $\endgroup$
    – Matt
    May 19, 2016 at 19:26

1 Answer 1

1
$\begingroup$

Simply create your thread with daemon=True and it will die when you exit Blender:

import threading

def runner():
    while True:
        print('Listening to port...')

thr = threading.Thread(target=blender_foil_guiappconnect, daemon=True)
thr.start()
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .