threading实现多线程的三种方法

  threading模块中Thread类是主要执行对象,可以有三种方法创建线程。1.给Thread实例传入一个函数。2.给Thread实力传如一个类实例。3.派生Thread子类创建一个子类的实例。

1.给Thread实例传入一个函数:

import threading
from time import sleep ,ctime

loops = [4,2]

def loop(nloop, nsec):
    print 'start loop', nloop, 'at:',ctime()
    sleep(nsec)
    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:
        threads[i].start()

    for i in nloops:
        threads[i].join()

    print 'all DONE at:', ctime()

if __name__ == '__main__':
    main()

相对于上一个博客里thread模块没有了锁的直接应用,而是调用join方法进行阻塞,并且线程调用start之后开始执行,线程之间便于同步。

2.传入一个可调用类实例给Thread的方法实现线程:

import threading
from time import sleep ,ctime

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, nsec):
    print 'start loop', nloop, 'at:',ctime()
    sleep(nsec)
    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:
        threads[i].start()

    for i in nloops:
        threads[i].join()

    print 'all DONE at:', ctime()

if __name__ == '__main__':
    main()

3.派生Thread子类并实例化:

import threading
from time import sleep ,ctime

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 run(self):
    self.func(*self.args)


def loop(nloop, nsec):
    print 'start loop', nloop, 'at:',ctime()
    sleep(nsec)
    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:
        threads[i].start()

    for i in nloops:
        threads[i].join()

    print 'all DONE at:', ctime()

if __name__ == '__main__':
    main()

总结:三种方法一般使用第三种方法就好,第一种封装性不好,第二种难于理解不利于代码阅读,我们可以把第三种方法普遍化一下

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 getResult(self):
    return self.res

    def run(self):
    print 'starting', self.name, 'at:', ctime()
    self.res = self.func(*self.args)
    print self.name, 'finished at:', ctime()
   

 

这段代码可以做为一个模块在程序里导入并使用它。

有时候为了有限的资源不被使用完,可以使用信号量设置最大的线程数量,当释放一次锁,信号量托盘获得它,即数目加一,若为0,不再执行代码。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值