Skip to content

Instantly share code, notes, and snippets.

@KabakiAntony
Created March 13, 2023 18:20
Show Gist options
  • Save KabakiAntony/c141b5b820ba548dcd014fb3eaf56493 to your computer and use it in GitHub Desktop.
Save KabakiAntony/c141b5b820ba548dcd014fb3eaf56493 to your computer and use it in GitHub Desktop.
thread class instance
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