I don't really understand the first question. When TimeoutError
is raised, no remaining work item finished in time - but that's obvious ;-)
About "it seems the result is printed even if the task times out": The tasks never time out - it's only waiting for a result that can time out. If no result is ready within a second, you loop around and try again. Eventually every work item does complete, so of course a result is eventually printed for every work item. You could change timeout=1
to, e.g., timeout=0.01
, and nothing about that would change (except you'd see Timeout
printed more often). TimeoutError
doesn't mean a task timed out - it only means that the main program timed out waiting for a task to finish. The tasks keep running.
For the second question, think about it more. Suppose, e.g., you start with 4 processes with sleep times of 1, 1, 3, and 3.
You wait a second and the first result is ready just before the 1-second timeout expires. The sleep time remaining in the other 3 processes decrease to 0, 2, and 2. While you're printing the first result, perhaps the first process starts a new work item with a sleep time of 1. So now the remaining wait times across all processes are 1, 0, 2, 2. In fact the 2nd process is already working on another new item, so the wait times remaining are 1, n
, 2, 2 for some value of n
.
The loop goes around and picks up the result from the 2nd process immediately. The wait times across processes are now a little less than 1, n
, 2, 2.
So waiting a second again picks up the result from the first process before the timeout, and the sleep times on the 3rd and 4th processes simultaneously fall below a second each.
And so on. Waiting a second for a result takes a second off each process's remaining sleep time simultaneously, because they're running concurrently.
I bet you'd see the behavior you expected if you changed the Pool constructor to say processes=1
. Then you'll see at least one timeout every time a process picks a sleep time of 2, and you'll see at least two timeouts every time a process picks a sleep time of 3. But when you're running 4 processes simultaneously, their remaining sleep times all decrease simultaneously.
Clearer?