python多线程
# 单线程执行耗时
import logging
from time import sleep, ctime
logging.basicConfig(level=logging.INFO)
def loop0():
logging.info("start loop at" + ctime())
sleep(4)
logging.info("end loop at" + ctime())
def loop1():
logging.info("start loop0 at" + ctime())
sleep(2)
logging.info("end loop0 at" + ctime())
def main():
logging.info("start all at" + ctime())
loop0()
loop1()
logging.info("end all at" + ctime())
if __name__ == '__main__':
main()
第一步 引入多线程_thread方法
# 调用_thread方法,进行优化,线程耗时缩短2s
# 但是实际项目执行中,sleep(6)未知(代码执行时间未知),改方法存在缺陷
import _thread
import logging
from time import sleep, ctime
logging.basicConfig(level=logging.INFO)
def loop0():
logging.info("start loop at" + ctime())
sleep(4)
logging.info("end loop at" + ctime())
def loop1():
logging.info("start loop0 at" + ctime())
sleep(2)
logging.info("end loop0 at" + ctime())
def main():
logging.info("start all at" + ctime())
_thread.start_new_thread(loop0, ())
_thread.start_new_thread(loop1, ())
sleep(6)
# loop0()
# loop1()
logging.info("end all at" + ctime())
if __name__ == '__main__':
main()
第二步 多线程_thread方法引用lock
# _thread引用lock,进一步进行优化代码逻辑
# 但是lock概念比较复杂,代码简洁和易读性不佳
import _thread
import logging
from time import sleep, ctime
logging.basicConfig(level=logging.INFO)
loops=[2, 4]
def loop(nloop, nsec, lock):
logging.info("start loop" + str(nloop) + "at" + ctime())
sleep(nsec)
logging.info("end loop" + str(nloop) + "at" + ctime())
lock.release()
# def loop1():
# logging.info("start loop0 at" + ctime())
# sleep(2)
# logging.info("end loop0 at" + ctime())
def main():
logging.info("start all at" + ctime())
# _thread.start_new_thread(loop0,())
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
# _thread.start_new_thread(loop1, ())
# sleep(6)
# loop0()
# loop1()
logging.info("end all at" + ctime())
if __name__ == '__main__':
main()
第三步 threading方法更加简洁
# threading方法执行多线程方法更加简洁了
# 把threading方法封装起来会不会更好?
import logging
import threading
from time import sleep, ctime
logging.basicConfig(level=logging.INFO)
loops=[2, 4]
def loop(nloop, nsec):
logging.info("start loop " + str(nloop) + " at" + ctime())
sleep(nsec)
logging.info("end loop " + str(nloop) + " at" + ctime())
def main():
logging.info("start all 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:
threads[i].start()
for i in nloops:
threads[i].join()
logging.info("end all at" + ctime())
if __name__ == '__main__':
main()
第四步 封装threading方法、重写run方法
# 单线程程序执行耗时,threading方法执行多线程
# 把threading方法封装起来,重写run方法,使得代码更加清晰易读
import logging
import threading
from time import sleep, ctime
logging.basicConfig(level=logging.INFO)
loops=[2, 4]
class MyThread(threading.Thread):
def __init__(self, func, args, name=''):
threading.Thread.__init__(self)
self.func = func
self.args = args
self.name = name
def run(self):
self.func(*self.args)
def loop(nloop, nsec):
logging.info("start loop " + str(nloop) + " at" + ctime())
sleep(nsec)
logging.info("end loop " + str(nloop) + " at" + ctime())
def main():
logging.info("start all 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:
threads[i].start()
for i in nloops:
threads[i].join()
logging.info("end all at" + ctime())
if __name__ == '__main__':
main()