title: asyncio: Is it possible to cancel a future been run by an Executor?
body: <p>I would like to start a blocking function in an Executor using the asyncio call loop.run_in_executor and then cancel it later, but that doesn't seem to be working for me.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import asyncio
  import time
  
  from concurrent.futures import ThreadPoolExecutor
  
  
  def blocking_func(seconds_to_block):
      for i in range(seconds_to_block):
          print('blocking {}/{}'.format(i, seconds_to_block))
          time.sleep(1)
  
      print('done blocking {}'.format(seconds_to_block))
  
  
  @asyncio.coroutine
  def non_blocking_func(seconds):
      for i in range(seconds):
          print('yielding {}/{}'.format(i, seconds))
          yield from asyncio.sleep(1)
  
      print('done non blocking {}'.format(seconds))
  
  
  @asyncio.coroutine
  def main():
      non_blocking_futures = [non_blocking_func(x) for x in range(1, 4)]
      blocking_future = loop.run_in_executor(None, blocking_func, 5)
      print('wait a few seconds!')
      yield from asyncio.sleep(1.5)
  
      blocking_future.cancel()
      yield from asyncio.wait(non_blocking_futures)
  
  
  
  loop = asyncio.get_event_loop()
  executor = ThreadPoolExecutor(max_workers=1)
  loop.set_default_executor(executor)
  asyncio.async(main())
  loop.run_forever()
  </code></pre>
  
  <p>I would expect the code above to only allow the blocking function to output:</p>
  
  <pre><code>blocking 0/5
  blocking 1/5
  </code></pre>
  
  <p>and then see the output of the non blocking function. But instead the blocking future continues on even after I have canceled.</p>
  
  <p>Is it possible? Is there some other way of doing it?</p>
  
  <p>Thanks</p>
  
  <p><strong>Edit:</strong> More discussion on running blocking and non-blocking code using asyncio: <a href="https://stackoverflow.com/questions/23898363/how-to-interface-blocking-and-non-blocking-code-with-asyncio">How to interface blocking and non-blocking code with asyncio</a></p>