Created
August 28, 2015 13:28
-
-
Save liftoff/165b7d4ad2879d1dc898 to your computer and use it in GitHub Desktop.
Example use of concurrent.futures
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
#!/usr/bin/env python | |
""" | |
Example of how to use futures with ProcessPoolExecutor or ThreadPoolExecutor. | |
""" | |
from concurrent import futures | |
def long_running_function(): | |
""" | |
Runs for 10 seconds and returns the current time when complete. | |
""" | |
import time | |
time.sleep(10) | |
return "current time.time(): %s" % time.time() | |
def all_done(future): | |
""" | |
Prints `future.result()` (what was returned by the future). | |
""" | |
print("All done! Result: '%s'" % future.result()) | |
if __name__ == "__main__": | |
print("Creating a ProcessPoolExecutor...") | |
executor = futures.ProcessPoolExecutor() # max_workers == number of cores by default | |
#executor = futures.ThreadPoolExecutor(max_workers=4) # Threaded way | |
print("Submitting our long-running function to the executor...") | |
future = executor.submit(long_running_function) | |
print("Adding a callback that will be called when that function completes...") | |
future.add_done_callback(all_done) | |
# You could proceed with more code at this point but because this is the | |
# last thing in our little script here it will automatically block until | |
# the last executor submission is complete. | |
# If you want to wait for multiple futures to complete you could use: | |
# futures.wait(list_of_futures, timeout=30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment