python 多线程
2013-11-27 16:38
330 查看
一、thread(不推荐)
二、threading
三、
1、Lock,RLock
2、Condition
3、Queue
4、threading.Event
5、thr.join([timeout]) 等待子线程执行结束
6、thr.setDaemon(bool) 设置子线程是否随主线程一起结束,必须在start()之前调用。默认为False。
7、getName(),setName(),isDaemon(),isAlive()
8、activeCount(),enumerate()
四、
import thread def foo(a): print a thread.start_new_thread(foo,(a))
二、threading
import threading class MyThread(threading.Thread): # def __init__(self,a): # threading.Thread.__init__(self[,name=xxx]) # self.a=a def run(self): pass thr=MyThread() thr.start()
三、
1、Lock,RLock
#主线程 mutex = threading.Lock() #子线程 mutex.acquire([timeout]) mutex.release()
2、Condition
#主线程 con = threading.Condition() #子线程 con.acquire() con.notify() con.wait() #... con.release()
3、Queue
import Queue #主线程 q=Queue.Queue() #FIFO,Maxsize #子线程 q.put(msg)#(item,block=True,timeout=None) q.get(msg) #q.qsize()==0
4、threading.Event
5、thr.join([timeout]) 等待子线程执行结束
6、thr.setDaemon(bool) 设置子线程是否随主线程一起结束,必须在start()之前调用。默认为False。
7、getName(),setName(),isDaemon(),isAlive()
8、activeCount(),enumerate()
四、
#coding=utf8 import threading class MyTread(threading.Thread): def run(self): while True: mylock.acquire() if len(task)==0: mylock.release() break t=task[0];del task[0] mylock.release() pass def main(): global task,mylock task=range(100) mylock=threading.Lock() for i in range(5): t=MyThread() # lt.append(t) t.start() for t in [li for li in threading.enumerate() if li.name!='MainThread']: #lt t.join() print threading.activeCount(),[li.name for li in threading.enumerate()] if __name__ == '__main__': main()
相关文章推荐
- 什么是Python
- python super() 方法使用
- python中文按拼音排序
- Ubuntu12.04下Opencv_Python 的安装
- python几道简单的算法题
- python mysqldb 数据库插入中文 “"UnicodeEncodeError:'latin-1' codec can't encode character ..."”
- python 从共享文件中自动拷贝指定文件到本地【续】
- python_收集服务器基本信息
- python 中关于xrange 和range
- python 标准库和第3方库的介绍
- Python与QueryDirets directory items(), keys(), values()
- python MySQLdb手动简单操作源码
- python 字符编码
- Python 特殊语法:filter、map、reduce、lambda
- python __import__
- python 函数
- Python学习,个人笔记,有点乱,不过比较全
- Python程序的性能分析指南
- python NameError: name 'raw_input' is not defined
- 简明Python教程 12)输入输出 13)异常