python线程学习

#!/usr/bin/python
#coding:utf-8
import time


# threading 提供了一个比thread模块更高层的API模块来提供线程的并发性。


# 下面表示一个程序的执行(这个例子需要5秒才能执行完成效率并不是很好)
"""
def worker(i):
    print 'worker%d'%i
    time.sleep(1)
    return


if __name__ == '__main__':
    aa = time.time()
    for x in xrange(5):
        worker(x)
    print time.time() - aa


# ############################
# 执行结果 :
# >>>
# worker0
# worker1
# worker2
# worker3
# worker4
# 5.04699993134
# ################################


"""
"""
# 下面的例子的是使用多线程




import threading


def worker(i):
    print 'worker%d'%i
    time.sleep(1)
    return


if __name__ == '__main__':
    aa = time.time()
    for x in xrange(5):
        t = threading.Thread(target=worker,args=(x,))   # target 是目标函数, args是传来的参数(用的元祖的形式进行传参)
        t.start()
    print time.time() - aa




#可以看到效率是很快的
# ############################
# 执行结果:
# worker0
# worker1
# worker2
# worker3
# worker4
# 0.000999927520752
# ############################


"""
"""
#2 threading.activeCount()的使用,此方法返回当前进程中线程的个数。返回的个数中包含主线程
import threading
import time


def worker(i):
    print 'worker%d'%i
    time.sleep(i)
    return


if __name__ == '__main__':
    aa = time.time()
    for i in xrange(6):
        t = threading.Thread(target=worker,args=(i,))
        t.start()


    print time.time() - aa
    print u'线程总数',threading.activeCount() - 1



"""


"""
# threading.enumerate()的使用。此方法返回当前运行中的Thread对象列表
import threading
import time


def worker(i):
    print 'worker%d'%i
    time.sleep(1)
    return


if __name__ == '__main__':
    threads = []
    for x in xrange(5):
        t = threading.Thread(target=worker,args=(x,))
        threads.append(t)
        t.start()


    for item in threading.enumerate():
        print item


    print "#################"


    for v in threads:
        print v
"""
"""
# threading.setDaemon()的使用。设置后台进程。


def  worker(i):
    time.sleep(1)
    print 'worker%s'%str(i)


if __name__ == '__main__':
    for i in xrange(5):
        t= threading.Thread(target=worker,args=(i,))
        t.setDaemon(True)
        t.start()


    print 'haha'


"""






import threading , time


# 继承线程函数
class MyThread(threading.Thread):
    """派生出的子线程 """
    def __init__(self,func,args,name=''):
        threading.Thread.__init__(self)          #初始化基类
        # 初始化函数
        self.func = func
        #初始化参数
        self.args = args
        #初始化名字
        self.name = name
    # 重新写线程的run函数
    def  run(self):
        apply(self.func,self.args)




def  threads(nloop,nsec):
    print 'start loop',nloop,'at',time.ctime()
    time.sleep(nsec)
    print 'loop',nloop,'done at',time.ctime()






def main():
    print 'starting at',time.ctime()
    ths = []
    nloops = range(5)
    #创建线程对象
    for i in nloops:
        t = MyThread(threads,(i,i),threads.__name__)
        ths.append(t)
    # 启动线程
    for i in nloops:
        ths[i].start()
    #等待子线程结束
    for i in nloops:
        ths[i].join()


    print 'all Done at',time.ctime()


if __name__ == '__main__':
    main()


# ############################
# >>>
# starting at Tue Dec 17 14:15:09 2013
# start loop 0 at Tue Dec 17 14:15:09 2013
# loop 0 done at Tue Dec 17 14:15:09 2013
# start loop 1 at Tue Dec 17 14:15:09 2013
# start loop 2 at Tue Dec 17 14:15:09 2013
# start loop 3 at Tue Dec 17 14:15:09 2013
# start loop 4 at Tue Dec 17 14:15:09 2013               上面标示线程同时进行了白哦是你应该开器了线程
# loop 1 done at Tue Dec 17 14:15:10 2013
# loop 2 done at Tue Dec 17 14:15:11 2013
# loop 3 done at Tue Dec 17 14:15:12 2013
# loop 4 done at Tue Dec 17 14:15:13 2013
# all Done at Tue Dec 17 14:15:13 2013
# ############################


参考地址:http://tuoxie174.blog.51cto.com/1446064/442162

http://blog.csdn.net/cnbird2008/article/details/4997920     




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值