_thread多线程编程实例:
#!/usr/bin/env python
# _*_ coding:UTF-8 _*_
import _thread
from time import sleep, ctime
def loop0():
print("Start loop 0 at: %s \r\n"% (ctime()))
sleep(4)
print("loop 0 done at: %s \r\n"% (ctime()))
def loop1():
print("Start loop 1 at: %s \r\n"% (ctime()))
sleep(4)
print("loop 1 done at: %s \r\n"% (ctime()) )
def main():
print ("starting at : %s \r\n"% (ctime()))
_thread.start_new_thread(loop0,())
_thread.start_new_thread(loop1,())
sleep(6)
print("All done at :%s \r\n"% (ctime()))
if __name__ == '__main__':
main()
输出结果如下:
>>>
======= RESTART: C:/work/python_learn/core python program/mtsleepA.py =======
starting at : Mon Jul 8 15:31:23 2019
Start loop 0 at: Mon Jul 8 15:31:23 2019
Start loop 1 at: Mon Jul 8 15:31:23 2019
loop 0 done at: Mon Jul 8 15:31:27 2019
loop 1 done at: Mon Jul 8 15:31:27 2019
All done at :Mon Jul 8 15:31:29 2019
>>>
添加锁之后使用_thread的code:
#!/usr/bin/env python
# _*_ coding:UTF-8 _*_
import _thread
from time import sleep, ctime
loops = [4, 4, 2]
def loop(nloop, nsec, lock):
print("Start loop %d at: %s \r\n"% (nloop, ctime()))
sleep(nsec)
print("loop %d done at: %s \r\n"% (nloop, ctime()))
lock.release()
def main():
print ("starting at : %s \r\n"% (ctime()))
locks = []
nloops = range(len(loops))
for i in nloops:
lock = _thread.allocate_lock()
lock.acquire()
locks.append(lock)
for i in nloops:
_thread.start_new_thread(loop, (i, loops[i], locks[i]))
for i in nloops:
while locks[i].locked():pass
print("All done at :%s \r\n"% (ctime()))
if __name__ == '__main__':
main()
执行结果如下所示:
>>>
======= RESTART: C:/work/python_learn/core python program/mtsleepB.py =======
starting at : Mon Jul 8 16:02:59 2019
Start loop 2 at: Mon Jul 8 16:03:00 2019
Start loop 0 at: Mon Jul 8 16:03:00 2019
Start loop 1 at: Mon Jul 8 16:03:00 2019
loop 2 done at: Mon Jul 8 16:03:02 2019
loop 0 done at: Mon Jul 8 16:03:04 2019
loop 1 done at: Mon Jul 8 16:03:04 2019
All done at :Mon Jul 8 16:03:04 2019
>>>
threading 多线程编程实例:
#!/usr/bin/env python
# _*_ coding:UTF-8 _*_
import threading
from time import sleep, ctime
loops = [4, 4, 2]
def loop(nloop, nsec):
print("Start loop %d at: %s \r\n"% (nloop, ctime()))
sleep(nsec)
print("loop %d done at: %s \r\n"% (nloop, ctime()))
def main():
print ("starting at : %s \r\n"% (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:
threads[i].start()
for i in nloops:
threads[i].join() #join()方法将等待线程结束
print("All done at :%s \r\n"% (ctime()))
if __name__ == '__main__':
main()
执行结果:
>>>
======= RESTART: C:/work/python_learn/core python program/mtsleepC.py =======
starting at : Mon Jul 8 16:27:51 2019
Start loop 0 at: Mon Jul 8 16:27:51 2019
Start loop 1 at: Mon Jul 8 16:27:51 2019
Start loop 2 at: Mon Jul 8 16:27:51 2019
loop 2 done at: Mon Jul 8 16:27:53 2019
loop 0 done at: Mon Jul 8 16:27:55 2019
loop 1 done at: Mon Jul 8 16:27:55 2019
All done at :Mon Jul 8 16:27:55 2019
>>>
单线程和多线程测试斐波那契、阶乘与累加函数的执行
#mode name myThread.py
#!/usr/bin/env python
# _*_ coding:UTF-8 _*_
import threading
from time import ctime
class MyThread(threading.Thread):
def __init__(self, func, args, name=''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
def getResulf(self):
return self.res
def run(self):
print("Start %s at %s"%(self.name, ctime()))
self.res = self.func(*self.args)
print("%s finished at %s"%(self.name, ctime()))
#!/usr/bin/env python
# _*_ coding:UTF-8 _*_
from myThread import MyThread
from time import ctime, sleep
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():
nfuncs = range(len(funcs))
print("*******Single Thread Result**********")
for i in nfuncs:
print("Start %s at %s \r\n"%(funcs[i].__name__, ctime()))
print(funcs[i](n))
print("%s finished at %s \r\n"%(funcs[i].__name__, ctime()))
print("========Multiple Threads=========")
threads = []
for i in nfuncs:
t = MyThread(funcs[i], (n,), funcs[i].__name__)
threads.append(t)
for i in nfuncs:
threads[i].start()
for i in nfuncs:
threads[i].join()
print("threads[i].getResulf() is %s \r\n"%(threads[i].getResulf()))
print("All done")
if __name__ == '__main__':
main()
执行结果:
======= RESTART: C:/work/python_learn/core python program/mtfacfib.py =======
*******Single Thread Result**********
Start fib at Mon Jul 8 17:47:42 2019
233
fib finished at Mon Jul 8 17:47:46 2019
Start fac at Mon Jul 8 17:47:46 2019
479001600
fac finished at Mon Jul 8 17:47:48 2019
Start sum at Mon Jul 8 17:47:48 2019
78
sum finished at Mon Jul 8 17:47:49 2019
========Multiple Threads=========
Start fib at Mon Jul 8 17:47:49 2019Start fac at Mon Jul 8 17:47:49 2019Start sum at Mon Jul 8 17:47:49 2019
fac finished at Mon Jul 8 17:47:50 2019sum finished at Mon Jul 8 17:47:50 2019
fib finished at Mon Jul 8 17:47:55 2019
threads[i].getResulf() is 233
threads[i].getResulf() is 479001600
threads[i].getResulf() is 78
All done
>>>