exit ThreadPoolExecutor immediately

Atul Johri al.johri at gmail.com
Mon Nov 14 03:51:49 EST 2016


I am looking for a way to stop a ThreadPoolExecutor immediately under the
assumption that I don't care about what's currently running or pending.

```
limit = 2
executor = ThreadPoolExecutor(10)
posts = itertools.islice(mygen(executor=executor, **kwargs), 0, limit)
for post in posts:
  print(post)
executor.shutdown(wait=False)
```

Basically I have a generator, mygen, which is using the executor to submit
many tasks in parallel and yield the result one at a time. I would like to
be able to limit the generator and have the executor stop processing
immediately.

I was considering clearing the _work_queue or iterating over it and running
future.cancel() on each future but both seem to be quite hacky and didn't
work in my initial try.

https://github.com/python/cpython/blob/master/Lib/concurrent/futures/thread.py#L83

Any ideas?

- Al



More information about the Python-list mailing list