0

So I use the .run_in_executor() method of the event loop to create a new process to perform my method:

executor = ProcessPoolExecutor(5)
list_of_futures = []

def run_in_another_process(func: Callable, *args, **kwargs) -> asyncio.Future:
    loop = asyncio.get_event_loop()
    future = loop.run_in_executor(executor=executor, func=functools.partial(func, *args, **kwargs))
    list_of_futures.append(future)
    return future

And it works fine. But when I try to cancel a future using cancel() method, it just does nothing, even though it's marked as canceled:

list_of_futures[0].cancel()

# True, but it's still in the RAM and is being performed!
list_of_futures[0].cancelled()

So the question is, how to cancel asyncio.Future properly? Why default method doesn't work?

Javad
  • 2,033
  • 3
  • 13
  • 23
Poision88
  • 43
  • 5
  • 1
    Does this answer your question? - https://stackoverflow.com/questions/26413613/asyncio-is-it-possible-to-cancel-a-future-been-run-by-an-executor – Tom McLean Jul 10 '22 at 13:17
  • 1
    Absolutely! Thank you so much, don't know how I didn't manage to find that. – Poision88 Jul 10 '22 at 13:36
  • 1
    Does this answer your question? [asyncio: Is it possible to cancel a future been run by an Executor?](https://stackoverflow.com/questions/26413613/asyncio-is-it-possible-to-cancel-a-future-been-run-by-an-executor) – Tom McLean Jul 10 '22 at 14:24

0 Answers0