Last active
February 1, 2021 19:22
-
-
Save benjiwheeler/ea19e3c5a016fd3d683d3fcf883fb639 to your computer and use it in GitHub Desktop.
python thread test
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
import time | |
import timeit | |
from multiprocessing.dummy import (Pool, TimeoutError) | |
def delay(mynum, delay_secs): | |
print("starting #%s" % mynum) | |
time.sleep(delay_secs) | |
print("done with #%s" % mynum) | |
return True | |
def call_functions_in_a_row(timeout): | |
pool = Pool(processes=3) | |
result1 = pool.apply_async(delay, [1, 10]) | |
print("call_functions_in_a_row: done kicking off #1, should take 10 seconds") | |
finished1 = result1.get(timeout=timeout) | |
print("call_functions_in_a_row: got result of #1") | |
result2 = pool.apply_async(delay, [2, 15]) | |
print("call_functions_in_a_row: done kicking off #2, should take 15 seconds") | |
finished2 = result2.get(timeout=timeout) | |
print("call_functions_in_a_row: got result of #2") | |
result3 = pool.apply_async(delay, [3, 5]) | |
print("call_functions_in_a_row: done kicking off #3, should take 5 seconds") | |
finished3 = result3.get(timeout=timeout) | |
print("call_functions_in_a_row: got result of #3") | |
print("call_functions_in_a_row: calling pool.close()") | |
pool.close() | |
print("call_functions_in_a_row: calling pool.terminate()") | |
pool.terminate() | |
print("call_functions_in_a_row: calling pool.join()") | |
pool.join() | |
print("reached end of call_functions_in_a_row") | |
def call_functions_at_same_time(timeout): | |
try: | |
pool = Pool(processes=3) | |
result1 = pool.apply_async(delay, [1, 10]) | |
print("call_functions_at_same_time: done kicking off #1, should take 10 seconds") | |
result2 = pool.apply_async(delay, [2, 15]) | |
print("call_functions_at_same_time: done kicking off #2, should take 15 seconds") | |
result3 = pool.apply_async(delay, [3, 5]) | |
print("call_functions_at_same_time: done kicking off #3, should take 5 seconds") | |
finished1 = result1.get(timeout=timeout) | |
print("call_functions_at_same_time: got result of #1") | |
finished2 = result2.get(timeout=timeout) | |
print("call_functions_at_same_time: got result of #2") | |
finished3 = result3.get(timeout=timeout) | |
print("call_functions_at_same_time: got result of #3") | |
except TimeoutError: | |
print("call_functions_at_same_time: hit timeout") | |
print("call_functions_at_same_time: calling pool.close()") | |
pool.close() | |
print("call_functions_at_same_time: calling pool.terminate()") | |
pool.terminate() | |
print("call_functions_at_same_time: calling pool.join()") | |
pool.join() | |
print("reached end of call_functions_at_same_time") | |
print("calling functions in a row, with timeout 20:") | |
start = timeit.default_timer() | |
call_functions_in_a_row(20) | |
stop = timeit.default_timer() | |
print("done calling functions in a row") | |
print("Total time: %s" % (stop - start)) | |
print("") | |
print("calling functions at the same time, with timeout 20:") | |
start = timeit.default_timer() | |
call_functions_at_same_time(20) | |
stop = timeit.default_timer() | |
print("done calling functions at the same time") | |
print("Total time: %s" % (stop - start)) | |
print("") | |
print("calling functions at the same time, with timeout 6: note THIS DOES NOT SEEM TO WORK:") | |
start = timeit.default_timer() | |
call_functions_at_same_time(6) | |
stop = timeit.default_timer() | |
print("done calling functions at the same time") | |
print("Total time: %s" % (stop - start)) | |
print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment