[Python-Dev] Status of thread cancellation

Nick Coghlan ncoghlan at gmail.com
Fri Mar 16 09:40:17 CET 2007


Josiah Carlson wrote:
> Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>> glyph at divmod.com wrote:
>>
>>> Can you suggest any use-cases for thread termination which will *not* 
>>> result in a completely broken and unpredictable heap after the thread 
>>> has died?
>> Suppose you have a GUI and you want to launch a
>> long-running computation without blocking the
>> user interface. You don't know how long it will
>> take, so you want the user to be able to cancel
>> it if he gets bored.
> 
> If the code is in Python, you can use sys.settrace to handle this. If
> the code is in an extension module that a user has control over, having
> a cancel_thread() function that is made available to Python, and having
> your C code check the value of a single variable every few seconds could
> do the same thing (even checking the value in a tight loop shouldn't
> slow computations down significantly, branch prediction should be able
> to make it a more or less zero-cost operation).  Yes, it can be tedious,
> but at least the programmer can actually control cleanup in a reasonable
> manner.

Option 3, farm the long running operation out to another process and use 
the OS-provided facilities to abort and cleanup if the user changes 
their mind. It's the only way to be sure the aborted operation doesn't 
leave the main process in a dodgy state.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list