python 多线程处理笔记

  1. 原始
import logging
from time import sleep,ctime
logging.basicConfig(level=logging.INFO)
#所有info级别的日志就会进行打印和输入
def loop0():
    logging.info("start loop0 at" + ctime())
    sleep(4)
    logging.info("end loop0 at" + ctime())

def loop1():
    logging.info("start loop1 at" + ctime())
    sleep(2)
    logging.info("end loop1 at" + ctime())

def main():
    logging.info("start all at" + ctime())
    loop0()
    loop1()
    logging.info("end all at" +ctime())

if __name__ == '__main__':
    main()

注:if name == ‘main’:

#一个python的文件有两种使用的方法,第一是直接作为脚本执行,第二是import到其他的python脚本中被调用(模块重用)执行。因此if name == ‘main’: 的作用就是控制这两种情况执行代码的过程,在if name == ‘main’: 下的代码只有在第一种情况下(即文件作为脚本直接执行)才会被执行,而import到其他脚本中是不会被执行的。

image

  1. 多线程优化 _thread
import logging
from time import sleep,ctime
import _thread
logging.basicConfig(level=logging.INFO)
#所有info级别的日志就会进行打印和输入
def loop0():
    logging.info("start loop0 at" + ctime())
    sleep(4)
    logging.info("end loop0 at" + ctime())

def loop1():
    logging.info("start loop1 at" + ctime())
    sleep(2)
    logging.info("end loop1 at" + ctime())

def main():
    logging.info("start all at" + ctime())
    _thread.start_new_thread(loop0,())
    _thread.start_new_thread(loop1,())
    sleep(6)
#当主线程退出的时候,所有的子线程会被强行退出,所以要加sleep(6),让主线程等6秒
    logging.info("end all at" +ctime())

if __name__ == '__main__':
    main()

image

  1. _thread多线程优化 用锁守护线程
import logging
from time import sleep,ctime
import _thread
logging.basicConfig(level=logging.INFO)
#所有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 main():
    logging.info("start all at" + ctime())
    locks=[]
    nloops = range(len(loops))
    for i in nloops:
        lock = _thread.allocate_lock()
        #声明一个锁
        lock.acquire()
        #加锁操作
        locks.append(lock)
        #locks追加一个lock
    for i in nloops:
        _thread.start_new_thread(loop,(i,loops[i],locks[i]))
    for i in nloops:
        while locks[i].locked():pass
    logging.info("end all at" +ctime())

if __name__ == '__main__':
    main()

image

  1. threading 优化
import logging
from time import sleep,ctime
import _thread
import threading
logging.basicConfig(level=logging.INFO)
#所有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()
        #等一个thread结束,若没有结束会阻塞main
    logging.info("end all at " +ctime())


if __name__ == '__main__':
    main()

image

  1. treading ,thread 改写,最推荐的一个方法
import logging
from time import sleep,ctime
import _thread
import threading
logging.basicConfig(level=logging.INFO)
#所有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):
        #重写run方法
        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()
        #等一个thread结束,若没有结束会阻塞main
    logging.info("end all at " +ctime())

if __name__ == '__main__':
    main()

image

到位。

关闭