So cute are you Python 9
来源:互联网 发布:d3.js 地球仪 编辑:程序博客网 时间:2024/06/08 08:28
1.线程的简单使用
#!/usr/bin/evn puthon#coding:utf-8#FileName:Rlock.py#Function: Show how to using threading with python#History:17-10-2013import threadingmylock = threading.RLock()num= 0 class myThread(threading.Thread): def __init__(self,name): threading.Thread.__init__(self) self.t_name=name def run(self): global num while True: mylock.acquire() print '\nThread(%s) lovked,Number:%d'%(self.t_name,num) if num>= 4: mylockrelease() print '\nThread(%s) released,Number:%d'%(self.t_name,num) break num+ 1 print '\nThread(%s) released,Number:%d'%(self.t_name,num) mylock.release()def test(): thread1 = myThread('A') thread2 =myThread('B') thread1.start() thread2.start() if __name__=='__main__': test()2.使用function 调用thread
#!/usr/bin/evn python#coding:utf-8#fileName:thread_a.py#function:show how to use an thread.#History:18-10-2013import string,threading,timedef thread_main(a): global count,mutex #获得线程名 threadname = threading.currentThread().getName() for x in xrange( 0,int(a)): #取得锁 mutex.acquire() count=count+ 1 #释放锁 mutex.release() print'Thread name:-%s,out_count=%d,count=%d' %(threadname,x,count) time.sleep( 1)def main(num): global count,mutex threads =[] count = 1 #创建一个锁 mutex=threading.Lock() #创建一个线程对象 for x in xrange( 0,num): threads.append(threading.Thread(target=thread_main,args=(10,))) #启动所有线程 for t in threads: t.start() #主线程等待所有子线程退出 for t in threads: t.join() if __name__ == '__main__': num= 4 #创建4个线程 main(num)3.使用 线程类
#!/usr/bin/evn python#coding:utf-8#fileName:thread_a.py#function:show how to use an thread ,with thread class#History:18-10-2013import threadingimport timeclass test(threading.Thread): def __init__(self,num): threading.Thread.__init__(self) self.__run__num=num def run(self): global count,mutex threadname=threading.currentThread().getName() for x in xrange( 0,int(self.__run__num)): mutex.acquire() count=count+ 1 mutex.release() print 'ThreadName:_%s,out_loop=%d,count_:%d'%(threadname,x,count) if __name__=='__main__': global count,mutex threads=[] num= 4 count= 1 #创建锁 mutex =threading.Lock() #创建对象 for x in xrange( 0,num): threads.append(test(10)) #启动线程 for t in threads: t.start() #等待子线程结束 for t in threads: t.join()
总结:我们会发现使用线程类时我们的线程要简洁一些。
- So cute are you Python 9
- So cute are you python 1
- So cute are you python 2
- So cute are you python 3
- So cute are you python 4
- So cute are you python 5
- So cute are you python 6
- So cute are you python 7
- So cute are you Python 8
- So cute are you Python 10
- So cute are you Python 11
- So cute are you Python 12
- So cute are you Python 13
- So cute are you python 14
- So cute are you python 15
- So cute are you python 16
- So cute are you python 17
- so cute are you python 18
- POJ 1286 Necklace of Beads
- WebSphere 各种dump
- RFC 879
- 绑定服务:调用远程服务中的方法
- linux 的一些命令指南
- So cute are you Python 9
- 混合调用和开启服务的生命周期
- GridView那点事之【表格背景色透明】
- 从图库选择一张图片
- Qt程序运行在mini2440开发板上
- struts2 + ajax + json的结合使用,实例讲解
- 按比例缩放分辨率大的图片
- 一位好的CEO需要征服的4个方向
- UVA - 10308 Roads in the North