Delay a computation in another thread

Thomas Jollans tjol at tjol.eu
Fri Sep 8 11:03:13 EDT 2017


On 2017-09-08 16:49, Steve D'Aprano wrote:
> 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?

On Linux, I think it would go to the same virtual terminal as it did
before, if that still exists. (Otherwise, to /dev/null). This is how
processes that started out attached to a terminal and then went into the
background normally behave.

No idea what happens on Windows, but if I had to guess, I'd say "not that".


-- 
Thomas Jollans



More information about the Python-list mailing list