-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultithreading.py
59 lines (50 loc) · 1.09 KB
/
multithreading.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
##import time
##
##
##def calc_square(numbers):
## print ("calculate the square numbers")
## for n in numbers:
## time.sleep(0.2)
## print 'square:',n*n
##
##
##
##def calc_cube(numbers):
## print ("calculate cube of numbers")
## for n in numbers:
## time.sleep(0.2)
## print 'cube:',n*n*n
##
##
##arr = [2,3,8,9]
##
##t = time.time()
##calc_square(arr)
##calc_cube(arr)
##
##print "done in:",time.time()-t
##print "Hah.... I am done with all my work now!"
####
#################################################################
import time
import threading
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 of numbers")
for n in numbers:
time.sleep(0.2)
print('cube:',n*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 work now!")