Delay a computation in another thread

Steve D'Aprano steve+python at pearwood.info
Fri Sep 8 10:49:34 EDT 2017


On Sun, 3 Sep 2017 03:03 am, MRAB wrote:

> On 2017-09-02 11:59, Steve D'Aprano wrote:
>> On Sat, 2 Sep 2017 08:53 pm, Steve D'Aprano wrote:
>> 
>>> I want to delay a computation and then print it, in the REPL (interactive
>>> interpreter). I have something like this:
>> [...]
>>> The other problem is that if I exit the REPL while a Timer is still active,
>>> it freezes until the time has run before exiting. I know you can't kill a
>>> thread from the main thread, but is there a way for the Timer to see that
>>> the interpreter is shutting down and shut itself down?
>> 
>> Oh! I see Timer objects have a cancel method. So I just need an atexit
>> callback function that calls cancel to each of the Timer threads.
>> 
> Timer is a subclass of Thread, so you can set its .daemon attribute.


Sorry for the long delay in replying to this, but if I set its daemon attribute,
won't that mean it will live on after the interpreter shuts down?

Also, what happens if it tries to print after the interpreter shuts down? Where
does output go?



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list