-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultithreading.py
162 lines (153 loc) · 4.16 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# import _thread
import threading
from time import ctime,sleep
##base test
# def loop0():
# print('start loop0 at ',ctime())
# sleep(4)
# print('loop0 done at ',ctime())
# def loop1():
# print('start loop1 at ',ctime())
# sleep(2)
# print('loop0 done at ',ctime())
# def main():
# print('starting at ',ctime())
# _thread.start_new_thread(loop0(),())
# _thread.start_new_thread(loop1(),())
# # loop0()
# # loop1()
# sleep(6)
# print('all done at ',ctime())
# if __name__=='__main__':
# main()
#threading
# loops=[4,2]
# def loop(nloop,nesc):
# print('start loop at ',ctime())
# sleep(nesc)
# print('loop',nloop,'done at',ctime())
#
# def main():
# print('starting at:', ctime())
# threads = []
# nloops = range(len(loops))
# for i in nloops:
# t=threading.Thread(target=loop,args=(i,loops[i]))
# threads.append(t)
# for i in nloops: #start threads
# threads[i].start()
# for i in nloops: #wait for all
# threads[i].join() #threads finish
# print('all done at ',ctime())
# if __name__=='__main__':
# main()
#threading class more object-oriented
# loops=[4,2]
# class ThreadFunc(object):
# def __init__(self,func,args,name=''):
# self.name=name
# self.func=func
# self.args=args
# def __call__(self):
# self.func(*self.args)
# def loop(nloop,nesc):
# print('start loop at ',ctime())
# sleep(nesc)
# print('loop',nloop,'done at',ctime())
#
# def main():
# print('starting at:', ctime())
# threads = []
# nloops = range(len(loops))
# for i in nloops:
# t=threading.Thread(
# target=ThreadFunc(loop,(i,loops[i]),loop.__name__))
# threads.append(t)
# for i in nloops: #start threads
# threads[i].start()
# for i in nloops: #wait for all
# threads[i].join() #threads finish
# print('all done at ',ctime())
# if __name__=='__main__':
# main()
#threading subclass
# loops=(4,2)
# class MyThread(threading.Thread):
# def __init__(self,func,args,name=''):
# threading.Thread.__init__(self)
# self.name=name
# self.func=func
# self.args=args
# def getResult(self):
# return self.res
# def run(self):
# print('starting',self.name,'at',ctime())
# self.res=self.func(*self.args)
# print(self.name,'finish at',ctime())
# def loop(nloop,nesc):
# print('start loop at ',ctime())
# sleep(nesc)
# print('loop',nloop,'done at',ctime())
#
# def main():
# print('starting at:', ctime())
# threads = []
# nloops = range(len(loops))
# for i in nloops:
# t=MyThread(loop,(i,loops[i]),loop.__name__)
# threads.append(t)
# for i in nloops: #start threads
# threads[i].start()
# for i in nloops: #wait for all
# threads[i].join() #threads finish
# print('all done at ',ctime())
# if __name__=='__main__':
# main()
#
#compare single and multithreading
class MyThread(threading.Thread):
def __init__(self,func,args,name=''):
threading.Thread.__init__(self)
self.name=name
self.func=func
self.args=args
def getResult(self):
return self.res
def run(self):
print('starting',self.name,'at',ctime())
self.res=self.func(*self.args)
print(self.name,'finish at',ctime())
def fib(x):
sleep(0.005)
if x<2:return 1
return (fib(x-2)+fib(x-1))
def fac(x):
sleep(0.1)
if x<2:return 1
return (x*fac(x-1))
def sum(x):
sleep(0.1)
if x<2:return 1
return (x+sum(x-1))
funcs=[fib,fac,sum]
n=12
def main():
nfunc=range(len(funcs))
print('---------single thread')
for i in nfunc:
print('starting',funcs[i].__name__,'at',ctime())
print(funcs[i](n),'what fuck')
print(funcs[i].__name__,'finish at ',ctime())
print('\n*****multiple threads')
threads=[]
for i in nfunc:
t=MyThread(funcs[i],(n,),funcs[i].__name__)
threads.append(t)
for i in nfunc:
threads[i].start()
for i in nfunc:
threads[i].join()
print(threads[i].getResult())
print('all done')
if __name__ == '__main__':
main()