python下线程以及锁
2015-07-19 23:17
716 查看
1、python多线程
2、python下的锁
#encoding=utf-8 """ python多线程,并非真正意义上的多线程 全局锁:在指定时间里,有且只有一个线程在运行 """ import threading import time def test(p): time.sleep(0.1) print p # a = threading.Thread(target=test) # b = threading.Thread(target=test) # a.start() # b.start() # # a.join() # b.join() lst =[] for i in xrange(0, 15): th = threading.Thread(target=test, args=[i]) lst.append(th) for i in lst: i.start() for i in lst: i.join() print "primary thread end!!!"
2、python下的锁
#encoding=utf-8 """ 1、python全局锁:GLT python多线程在任意时刻下只有一个线程在运行,它是线程安全的 """ import threading num = 0 def t(): global num num +=1 print num for i in xrange(0, 10): d = threading.Thread(target=t) d.start() import time b_time = time.time() _a = threading.Thread(target=t) _b = threading.Thread(target=t) _a.start() _b.start() _a.join() _b.join() print time.time()-b_time """ 加锁:acquire() 解锁:release() RLock()可重入锁 """ import threading mlock = threading.Lock() #mlock = threading.RLock() num_01 = 0 def a(): global num_01 mlock.acquire() num_01 += 1 mlock.release() print num_01 for i in xrange(0, 10): d = threading.Thread(target=a) d.start()
相关文章推荐
- python下yield(生成器)
- python while语句
- python 魔术方法
- Python lambda函数的用法
- python if语句
- Python笔记——break的注意事项
- Python笔记——break的注意事项
- 学习两天python后感
- 如何写出优雅的Python
- python 输出中文乱码问题
- python读取配置文件
- python的高级特性3:神奇的__call__与返回函数
- Berkeley's SICP in python3(三)
- pyenv管理python多版本
- Python解决Google文档打开缓慢问题
- python字典--知识点总结
- python字典--知识点总结
- Python dict 字典
- python程序中文输出问题怎么解决
- Python生成验证码