From: MRAB on
pacopyc wrote:
> On 19 Giu, 00:27, MRAB <pyt...(a)mrabarnett.plus.com> wrote:
>> pacopyc wrote:
>>
>> [snip]
>>> Ok, the problem is that I want fix a time max for each thread. For
>>> example run 10 threads (each can terminate its work in 10 sec. max
>>> fixed time) and wait them. If they finish its work in < 10 sec. (for
>>> example 2 sec.) very good ... go on immediately (don't wait
>>> unnecessary time), but if a thread use more than 10 sec. stop wait (I
>>> kill it) .... when all threads have finished their work or when they
>>> have used all their available time (10 sec.) the program must go on
>>> (don't wait).
>> It's not possible to kill a thread. If you want a thread to have a
>> maximum time, the thread must check occasionally how long it has been
>> running and terminate if necessary.
>>
>> Another programming language (Java) originally had the ability to kill
>> threads, but that was later deprecated because it caused problems due to
>> not knowing what the thread was doing when it was killed (it might have
>> been in the middle of updating something at the time, for example,
>> leaving the system in an inconsistent state).
>
> Ok, I understand. But is possible fix max wait time (for example 60
> sec.) in main thread and check queue for task done and at the same
> time the remaining time. When all task have done or wait time has
> expired main thread go on. Is it possible? Can you write code?
> Thank you very much.

The documentation says that queue.join() can't have a timeout, so you
might have to think of another way to achieve the same effect.

You could, for example, have a results queue into which a thread puts
something to indicate when it has finished, and then use the .get method
on it in the main thread (the .get method can have a timeout).