Skip to content

Instantly share code, notes, and snippets.

@santosh
Last active January 14, 2018 10:23
Show Gist options
  • Save santosh/6289455999e5690d79e7772f32117231 to your computer and use it in GitHub Desktop.
Save santosh/6289455999e5690d79e7772f32117231 to your computer and use it in GitHub Desktop.
Starting threads. #2
import threading
import time
def calc_square(numbers):
print("calculate square numbers")
for n in numbers:
time.sleep(0.2)
print('square', n*n)
def calc_cube(numbers):
print("calculate cube numbers")
for n in numbers:
time.sleep(0.2)
print('cube', n*n)
arr = [2, 3, 8, 9]
t = time.time()
t1 = threading.Thread(target=calc_square, args=(arr,))
t2 = threading.Thread(target=calc_cube, args=(arr,))
t1.start()
t2.start()
t1.join()
t2.join()
print("done in: ", time.time() - t )
print("Hah.. I am done with all my works now!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment