-
Notifications
You must be signed in to change notification settings - Fork 44
/
Chapter_13th.py
63 lines (54 loc) · 1.63 KB
/
Chapter_13th.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
60
61
62
63
# import libraries
import logging
import time
import datetime
import threading
def get_cubic(num):
print(datetime.datetime.now())
print("Cube: {}".format(num * num * num))
def get_inverse(num):
print(datetime.datetime.now())
print("Inverted: {}".format(1 / num))
# Example 1
# main execution
if __name__ == "__main__":
# creating thread
t1 = threading.Thread(target=get_cubic, args=(10,))
t2 = threading.Thread(target=get_inverse, args=(10,))
# Starting the threads
t1.start()
t2.start()
# waiting for each to finish
t1.join()
t2.join()
# both threads completely executed
print("Done!")
# Output
# Cube: 1000
# Inverted: 0.1
# Done!
# Example 2
if __name__ == "__main__":
# creating thread
t1 = threading.Thread(target=get_cubic, args=(10,))
t2 = threading.Thread(target=get_inverse, args=('10',))
# Starting the threads
t1.start()
t2.start()
# waiting for each to finish
t1.join()
t2.join()
# both threads completely executed
print("Done!")
# Output
# Cube: 1000
# Done!
# Exception in thread Thread-16:
# Traceback (most recent call last):
# File "C:\Users\anaconda3\lib\threading.py", line 926, in _bootstrap_inner
# self.run()
# File "C:\Users\anaconda3\lib\threading.py", line 870, in run
# self._target(*self._args, **self._kwargs)
# File "E:\Python task\Python\4.8.Threading.py", line 12, in get_inverse
# print("Inverted: {}".format(1 / num))
# TypeError: unsupported operand type(s) for /: 'int' and 'str'