ThreadPoolExecutor - callback guaranteed to run in same thread as the submitted function?

Ian Kelly ian.g.kelly at gmail.com
Thu Sep 25 08:39:30 CEST 2014


On Wed, Sep 24, 2014 at 8:44 AM, Praveen Gollakota <pgollakota at gmail.com> wrote:
> However, it seemed to fail when I removed the `time.sleep(random.random())` statements, i.e. at least a few `func` functions and `callbacks` **did not** run in the same thread.
>
> For a project that I am working on, the callback must always run on the same thread as the submitted function, so I wanted to be sure that this is guaranteed by TPE. (And also the results of the test without the random sleep seemed puzzling).

The documentation for Future.add_done_callback says: "If the future
has already completed or been cancelled, fn will be called
immediately." Checking the code, the callback is simply called from
the submitting thread in this case. This makes sense as the thread
that completed the future has already moved onto something else and
may not be readily available or even identifiable.



More information about the Python-list mailing list