On Thu, Sep 25, 2014 at 3:46 AM, Marko Rauhamaa <ma...@pacujo.net> wrote: > Ian Kelly <ian.g.ke...@gmail.com>: > >> The documentation for Future.add_done_callback says: "If the future >> has already completed or been cancelled, fn will be called >> immediately." > > That sounds really bad. > > There should be a guarantee that the callback is not called from the > same thread before returning from Future.add_done_callback. Otherwise, > the caller may have a really difficult time handling preemption and > avoiding deadlocks at the same time. > > Example (pseudocode): > > def callback(self): > with self.lock: > ... > > def xyz(self, f): > with self.lock: > ... > f.add_done_callback(self.callback) > > The code will deadlock if the callback is invoked immediately.
Easily solved using a re-entrant lock. Alternatively, one could use synchronization to make sure the future can't complete until all callbacks have been added. That would have the benefit of keeping the callback processing on the same thread as the future, as the OP wanted. -- https://mail.python.org/mailman/listinfo/python-list