Created
March 13, 2023 18:20
-
-
Save KabakiAntony/c141b5b820ba548dcd014fb3eaf56493 to your computer and use it in GitHub Desktop.
thread class instance
This file contains 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 | |
from threading import Thread | |
def add_two_numbers(a, b): | |
return a + b | |
def simulate_api_call(name): | |
print(f'{name} is sleeping for 3 seconds') | |
time.sleep(3) | |
print(f"{name} is done sleeping") | |
def main(): | |
start = time.perf_counter() | |
api_caller_threads = [] | |
for i in range(1, 3): | |
thread = Thread(target=simulate_api_call, args=(f'Thread {i}',)) | |
thread.start() | |
api_caller_threads.append(thread) | |
sum = add_two_numbers(2,5) | |
print(f'The sum is {sum}') | |
for api_caller_thread in api_caller_threads: | |
api_caller_thread.join() | |
finish = time.perf_counter() | |
print(f'Finished in {finish-start: 0.2f} second(s)') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment