<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
FONT-SIZE: 10pt;
FONT-FAMILY:Tahoma
}
</style>
</head>
<body class='hmmessage'>[OK so a newbie post here so many apologies if I am doing this wrong]<BR><BR>
Quick Synopsis:<BR>
&nbsp;<BR>
&nbsp;A child thread in an executing Python program can not safely shutdown the program. The issue URL is: <A href="http://bugs.python.org/issue502236">http://bugs.python.org/issue502236</A><BR>
&nbsp;<BR>
So my proposal is:<BR>
&nbsp;<BR>
Example:<BR>
&nbsp;<BR>
&nbsp; We have three threads -<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t0 - Main system thread<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t1 - Worker thread<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t2 - Worker thread<BR>
&nbsp;<BR>
&nbsp; t1 encounters an issue that means it wants to shut down the application in as safe a way as possible<BR>
&nbsp;<BR>
&nbsp;<BR>
A Solution:<BR>
&nbsp;<BR>
&nbsp;1. Put in place a new function call sys.exitapplication, what this would do is:<BR>
&nbsp;&nbsp;&nbsp;&nbsp; a. Mark a flag in t0's&nbsp;data structure saying a request to shutdown has been made<BR>
&nbsp;&nbsp;&nbsp;&nbsp; b. Raise a new exception, SystemShuttingDown, in t1.<BR>
&nbsp;2. As the main interpreter executes it checks the "shutting down flag" in the per thread data and follows one of two paths:<BR>
&nbsp;&nbsp;&nbsp; If it is t0:<BR>
&nbsp;&nbsp;&nbsp;&nbsp; a. Stops execution of the current code sequence<BR>
&nbsp;&nbsp;&nbsp;&nbsp; b. Iterates over all extant threads setting the "system shutdown" flag in the per thread data structure. Setting this flag is a one time deal - it can not be undone once set. (And to avoid issues with multiple threads setting it - it can only ever be a single fixed value so setting it multiple times results in the same answer)<BR>
&nbsp;&nbsp;&nbsp;&nbsp; c. Enters a timed wait loop where it will allow the other threads time to see the signal. It will iterate this loop a set number of times to avoid being blocked on any given thread.<BR>
&nbsp;&nbsp;&nbsp;&nbsp; d. When all threads have exited, or been forcefully closed, raise the SystemShuttingDown exception<BR>
&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;If it is not t0:<BR>
&nbsp;&nbsp;&nbsp;&nbsp; a. Stops execution of the current code sequence<BR>
&nbsp;&nbsp;&nbsp;&nbsp; b. Raises the exception, SystemShuttingDown.<BR>
&nbsp;<BR>
There are problems with this approach, as I see it they are (but please see the assumptions I have made):<BR>
&nbsp;<BR>
P1. If the thread is in a tight loop will it see the exception? Or more generally: when should the exception be raised?<BR>
P2. When should the interpreter check this flag?<BR>
&nbsp;<BR>
I think the answer to both of these problems is to:<BR>
&nbsp;<BR>
&nbsp;Check the flag, and hence raise the exception, in the following circumstances:<BR>
&nbsp;<BR>
&nbsp; - When the interpreter executes a back loop. So this should catch the jump back to the top of a "while True:" loop<BR>
&nbsp; - Just before the interpreter makes a call to a hooked in non-Python system function, e.g. file I/O, networking &amp;c.<BR>
&nbsp;<BR>
&nbsp;Checking at these points should be the minimal required, I think, to ensure that a given thread can not ignore the exception. It may be possible, or even required, to perform the check every time a Python function call is made.<BR>
&nbsp;<BR>
I think this approach would then allow for the finally handlers to be called.<BR>
&nbsp;<BR>
Assumptions:<BR>
&nbsp;<BR>
[Here I must admit to a large amount of ignorance of the internals of Python at this time. So if my assumptions are incorrect I would greatly appreciate being told so :-) Preferably as polite as possible and any code pointers while welcome unless they point to some very esoteric and arcane area would be best kept general so I feel more of a spur to go learn the code base]<BR>
&nbsp;<BR>
&nbsp;1. The Python interpreter has per thread information.<BR>
&nbsp;2. The Python interpreter can tell if the system, t0, thread is running.<BR>
&nbsp;3. The Python engine has (or can easily obtain) a list of all threads it created.<BR>
&nbsp;4. It is possible to raise exceptions as the byte code is executing.<BR>
&nbsp;<BR>
I am mailing this out as:<BR>
&nbsp;<BR>
&nbsp;A. I have no idea if my thoughts are correct or total un-mitigated rubbish :-)<BR>
&nbsp;B. I believe the introduction of this proposal (if I am correct) will require a PEP being raised, which aiui requires building community support (which is very fair imo) so this is me trying to do so :-)<BR>
&nbsp;<BR>
So apologies if this post has been total spam (but no eggs) or too long - give a little whistle and it will all be OK again.<BR>
&nbsp;<BR>
Andy<BR>
--------------------------------------<BR>Brain chemistry is not just for Christmas<BR><BR><br /><hr />Get Messenger on your Mobile! <a href='http://clk.atdmt.com/UKM/go/101719964/direct/01/' target='_new'>Get it now!</a></body>
</html>