Skip to content

Instantly share code, notes, and snippets.

@wfng92
Created April 10, 2020 07:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wfng92/d840c3a2ec5f72c3fb87c4364021283a to your computer and use it in GitHub Desktop.
Save wfng92/d840c3a2ec5f72c3fb87c4364021283a to your computer and use it in GitHub Desktop.
from concurrent.futures import ProcessPoolExecutor
import time
def wait_function(x, y):
print('Task(', x,'multiply', y, ') started')
time.sleep(2)
print('Task(', x,'multiply', y, ') completed')
return x * y
def callback_function(future):
print('Callback with the following result', future.result())
def main():
with ProcessPoolExecutor(max_workers=2) as executor:
future = executor.submit(wait_function, 3, 4)
future.add_done_callback(callback_function)
future2 = executor.submit(wait_function, 8, 8)
while True:
if(future.running()):
print("Task 1 running")
if(future2.running()):
print("Task 2 running")
if(future.done() and future2.done()):
print(future.result(), future2.result())
break
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment