Last active
February 2, 2026 02:38
-
-
Save davidlares/4eed08488c18a1d2c86cb225eaf119a2 to your computer and use it in GitHub Desktop.
Python's Future object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from concurrent.futures import Future | |
| import threading | |
| import requests | |
| import logging | |
| import time | |
| # logging configuration | |
| logging.basicConfig( | |
| level=logging.DEBUG, | |
| format='%(thread)s %(threadName)s : %(message)s' | |
| ) | |
| def callback_future(future): # future param (necessary) | |
| logging.info('Hi, Im a callback executed until the future have a certain value') | |
| logging.info(f'The future is {future.result()}') | |
| if __name__ == "__main__": | |
| future = Future() | |
| # setting the callback for execution | |
| future.add_done_callback(callback_future) | |
| # can be more callbacks to the future, the 'result' param is necessary | |
| future.add_done_callback( | |
| lambda future: logging.info('Lambda here') | |
| ) | |
| logging.info('Starting a complex task') | |
| time.sleep(2) | |
| # future data | |
| future.set_result('david') | |
| logging.info('Future set with value') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment