|
From: Ype K. <yk...@xs...> - 2001-10-14 09:06:48
|
Samuele, > > Matt, >> >> >Hi Folks, >> > >> >We love Jython. We're trying to provide some functionality to our users that >allows them to start a Jython interpreter, user our system (by instatiating a >class), then quit. However, we need to have a cleanup method run when the >interpreter exits. Is there a way to do this? THanks. >> >> In jython: use the atexit module, or use try/finally around the >> exec or execfile() around the user code invocation. >> In java: use try/finally around the call to the jython interpreter. > >Maybe I'm just very confused, but despite of the News >jython does not support effectively atexit . I have run into the problem that when using swing it is necessary to call java.lang.System.exit() to exit the process running the jython interpreter. (This was under OS2, and it seems that swing under OS2 registers some exit hooks to the JVM that are crucial for the well being of the GUI after the JVM exits.) I solved it by writing a jython function calling java's exit function and registering this jython function as the first atexit function. It works fine, the jython exit exception is nicely caught. I was a bit surprised that calling java.lang.System.exit() isn't done by default (*), but then I realised that jython is an implementation of the python language and this behaviour is not part of the language. In Python style it could for example be done in the atexit module, or it might be optional behaviour. When you say "does not support effectively atexit" do you mean that there some limitations that I did not encounter? Thinking about it a bit more I would actually like to have java.lang.System.exit() called by default when a process based on a jython interpreter exits. All java processes are supposed to do this, and I cannot think of a reason why jython processes should be an exception. If necessary, one can always call the java exit function that does not use the JVM's exit hooks. Regards, Ype (*) in jython 21a1 iirc, i havn't checked later versions, but the atexit module still works fine for me in 21a3. |