This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: concurrent.futures: executor.submit() runs until completion even when future times out or is canceled
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: bquinlan, jenisys, pitrou
Priority: normal Keywords:

Created on 2011-03-30 21:00 by jenisys, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_futures_more3.py jenisys, 2011-03-30 21:09 unit test to show observed behaviour
Messages (3)
msg132614 - (view) Author: (jenisys) Date: 2011-03-30 21:00
A long running task that is submitted to an ThreadPoolExecutor runs to completion even when the future times out or is canceled.
Even executor.shutdown(wait=False) will not help.

HOW TO REPEAT:
Provide a function which sleeps for 10 seconds.
Submit it to a ThreadPoolExecutor.
Wait on the future for the  result for 2 seconds.

OBSERVED BEHAVIOUR:
1. future times out after approx. 2 seconds
2. Process shutdown is delayed by an additional 8 seconds,
   meaning it waits until submitted task/function has completed.
msg132635 - (view) Author: Brian Quinlan (bquinlan) * (Python committer) Date: 2011-03-31 04:31
This is the expected behavior.

Future.cancel() returns a bool indicating if the future was cancelled or not. If it returns False then there is no way to stop it.

The behavior that you are seeing for shutdown is documented as:
"""
shutdown(wait=True)
...

If wait is True then this method will not return until all the pending futures are done executing and the resources associated with the executor have been freed. If wait is False then this method will return immediately and the resources associated with the executor will be freed when all pending futures are done executing. Regardless of the value of wait, the entire Python program will not exit until all pending futures are done executing.
"""
msg132658 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-03-31 12:06
Agreed with Brian. There is generally no reliable way to terminate another thread or process without cooperation from said thread or process. Especially in the case of threads, terminating a thread while leaving the process alive may leave some resources (including Python interpreter resources) in a consistent state. Admittedly, terminating a process could be implemented using kill().

The solution is to write your long-running task so that it can be cancelled using a synchronization object (such as an Event).
History
Date User Action Args
2022-04-11 14:57:15adminsetgithub: 55933
2011-03-31 12:06:29pitrousetstatus: open -> closed
nosy: + pitrou
messages: + msg132658

2011-03-31 04:31:56bquinlansetresolution: not a bug
messages: + msg132635
2011-03-31 00:43:20ned.deilysetnosy: + bquinlan
2011-03-30 21:09:40jenisyssetfiles: - test_futures_more3.py
2011-03-30 21:09:34jenisyssetfiles: + test_futures_more3.py
2011-03-30 21:05:05jenisyssetfiles: - test_futures_more3.py
2011-03-30 21:04:59jenisyssetfiles: + test_futures_more3.py
2011-03-30 21:02:38jenisyssetfiles: + test_futures_more3.py
2011-03-30 21:00:08jenisyscreate