6

This little snippets prints False

import subprocess
from concurrent import futures
with futures.ThreadPoolExecutor(max_workers=1) as executor:
    future=executor.submit(subprocess.call, ['sleep', '2'])
    print(future.cancel())

According to the docs this means "future can't be canceled": https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Future.cancel

cancel(): Attempt to cancel the call. If the call is currently being executed and cannot be cancelled then the method will return False, otherwise the call will be cancelled and the method will return True.

Is there a way to cancel the future although the cancel() methods tells me it can't be canceled?

I am running the backport on Python 2.7

guettli
  • 25,042
  • 81
  • 346
  • 663
  • 2
    You're not actually passing `subprocess.call` as a function to be run by your `ThreadPoolExecutor`, but instead calling it immediately and passing its return value to `submit()`. That's probably not what you intend to be doing! – Blckknght Jun 16 '16 at 10:12
  • @Blckknght yes, you are right. This was typo, I updated the question. Thank you for finding this mistake. – guettli Jun 16 '16 at 13:13
  • 1
    @GáborFekete yes, it is a duplicate. – guettli Jun 16 '16 at 14:36
  • I'm guessing if you did `for i range(99): ex.submit(cpu_intensive_task);` and only then `ex.submit(subprocess.call, ...)` then you'd be able to cancel the latter. The point here is subprocess is meant to have side-effects (imagine `s/sleep/fdisk/`). There's no good point to cancel it at. How about you write a cancellable subprocess.Popen implementation that would kill the child on cancel? – Dima Tisnek Sep 08 '16 at 17:48

0 Answers0