Frage

i'm using the OSC library found here. i'd like to programmatically exit the program via sys.exit() when a specific msg is received.

i use something like the following:

oserve = OSC.OSCServer(('localhost', iportarg))
st = threading.Thread(target = oserve.serve_forever)
oserve.addMsgHandler("/logout", logout_handle)

def logout_handle(addr, tags, stuff, source):
    sys.exit()

issuing the message '/logout' yields the following error:

OSCServer: SystemExit on request from localhost:55827:

does anyone have any idea why that is? am in within a sub-process that has been thread-locked? it seems like the OSC lib/module won't allow me to issue a system process while it's still active. any ideas would be great...

thanks, jml

War es hilfreich?

Lösung

Can I suggest an alternative approach, use a flag done=False loop until done is True then cleanup by killing the st thread and then exiting.

from gevent import spawn
from gevent.event import AsyncResult
is_done = AsyncResult()
oserve = OSC.OSCServer(('localhost', iportarg))
gl = gevent.spawn(oserve.oserve_forever)
oserve.addMsgHandler("/logout", logout_handle)

def logout_handle():
  global is_done
  is_done.set(True)

terminate_prog = False
while not terminate_prog:
    if is_done.ready():
        terminate_prog = True


st.kill()
sys.exit()

Something like this is a little more preferable imo and you can properly cleanup resources.

What we're doing is making an AsyncResult that both greenlets can communicate with, the main greenlet loops until it's ready to quit listening for that async result, the other greenlet has a hook where it executes logout_handle eventually which signals the main greenlet it's time to quit.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top