Skip to content

Instantly share code, notes, and snippets.

@liftoff
Created August 28, 2015 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liftoff/165b7d4ad2879d1dc898 to your computer and use it in GitHub Desktop.
Save liftoff/165b7d4ad2879d1dc898 to your computer and use it in GitHub Desktop.
Example use of concurrent.futures
#!/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