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

Chris Angelico rosuav at gmail.com
Thu Sep 25 12:04:40 EDT 2014


On Fri, Sep 26, 2014 at 1:40 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> Fair enough. In the simple example above, I see no harm in simply
> moving the add_done_callback call outside (after) the "with self.lock"
> block altogether. I can imagine more complex examples where the call
> is further down the stack and harder to isolate from the lock,
> although I would argue that's probably a code smell.

I'd definitely call it code smell. What happens with this?

with lock:
    def inner():
        do_more_work()
    f.add_done_callback(inner)

The lock's possibly released before the callback happens, or possibly
not, but either way, the code *looks* as if it's all happening inside
the "with lock" block. Recipe for confusion IMO.

ChrisA



More information about the Python-list mailing list