from concurrent.futures import Future
def done_callback(f):
print("done callback is invoked")
f = Future()
f.add_done_callback(done_callback)
f.set_result('first set')
f.set_result('second set')
the output is:
done callback is invoked
done callback is invoked
I think that future can only be set once, it's better. and in this case, done callbacks will be invoked only once.
class Future(object):
...
def set_result(self, result):
"""Sets the return value of work associated with the future.
Should only be used by Executor implementations and unit tests.
"""
with self._condition:
if self.done():
return
...
def set_exception_info(self, exception, traceback):
"""Sets the result of the future as being the given exception
and traceback.
Should only be used by Executor implementations and unit tests.
"""
with self._condition:
if self.done():
return
...
below is an example:
the output is:
I think that future can only be set once, it's better. and in this case, done callbacks will be invoked only once.