-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.py
122 lines (76 loc) · 2.45 KB
/
thread.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import threading
import time
# def g():
# print("thread is working ! ")
# t = threading.Thread(target=g)
# t.start()
# def f(i) :
# print("the i is :" , i )
# for i in range(3):
# threading.Thread(target=f , args=(i,)).start()
# def h(i , name = 'default'):
# print('thread named {} with id {} is working'.format(name,i))
# for i in range(0,3):
# threading.Thread(target=h ,args=(i,) , kwargs={'name': '{}{}{}'.format(i,i,i)}).start()
# How to find out which thread is working right now ? :
# def t():
# print('current thread is: ' + threading.current_thread().name)
# print(threading.currentThread().getName())
# threading.Thread(name ='noTime' , target=t)
# threading.Thread(name ='Hossein' , target=t).start()
# threading.Thread(name ='kkowsar' , target=t).start()
# threading.Thread(target=t).start()
# How to make deamon thread :
#non- daemon :
# def func():
# # print('(' +threading.current_thread().name +') started ! ' )
# time.sleep(1)
# #print('(' +threading.current_thread().name +') finished ! ' )
# t = threading.Thread(name ='non-deamon' , target=func)
# t.start()
# daemon :
# def func2():
# print('(' +threading.current_thread().name +') started ! ' )
# time.sleep(1)
# print('(' +threading.current_thread().name +') finished ! ' )
# t = threading.Thread(name = 'non-daemon', target=func2)
# t.setDaemon(True)
# t.start()
# # time.sleep(2)
# print("this is main thread")
# join() in threading :
# def f():
# print('(' +threading.current_thread().name + ') started !')
# time.sleep(2)
# print('(' +threading.current_thread().name + ') finished !')
# t = threading.Thread(name ='firstOne',target=f)
# t.start()
# t.join()
# print('salam')
# print(t.is_alive())
# print("______________________")
# t = threading.Thread(name ='secondOne', target=f)
# t.start()
# t.join(1.0)
# print('khoobi?')
# print(t.is_alive())
# print("---------------------")
# t = threading.Thread(name ='thirdOne',target=f)
# t.start()
# print('che khabar?')
# print(t.is_alive())
# print("---------------------")
# enumerate :
def f(i) :
print('(' +threading.current_thread().name + ') started')
time.sleep(i)
print('(' +threading.current_thread().name + ') finished')
ls = []
for i in range (1,5):
ls.append(threading.Thread(name='t'+str(i), target=f, args=(i,)))
for t in ls :
t.start()
time.sleep(3)
print('')
for t in threading.enumerate():
print(t.name + "is alive now ")