在 threading 中,主要有下面几个 class:
- threading.Thread 对象是主要的创建线程的工具,一般我们实现其 Thread.run 方法即可,然后调用 Thread.start() 开始在新的线程里面执行 Thread.run 的内容,其 join 方法用于等候其他线程结束。我们可以设置其 name 和 daemon 属性;
- threading.Lock 对象用于设置锁,通过 Lock.acquire() 方法与 Lock.release() 方法进行对某些需要串行访问对象的保护;
- threading.RLock 是 reentrant lock,允许同一个线程多次上锁;
- threading.Condition 用于创建条件同步,它需要一个 Lock 或者 RLock 才能构造,条件满足后,可以通过 notify() 或者 notifyAll() 让所有等候的线程从挂起变成继续执行状态;
- threading.Semaphore 用于创建所谓的 semaphore 对象,一旦该对象的值为 0 就会将需要获得的线程挂起,这往往用作设定最大连接数后,启动线程去完成某些任务;
- threading.Event 对象用于创建所谓的事件,通过 wait 方法等到某个线程调用 set 时就会被结束等待;
- threading.Timer 与 timer 模块不同,这是用 thread 实现的,而不是用系统的那个;可以用来在某个时间执行某个函数;
下面是一个简单的使用多线程的 hello world。
#!/usr/bin/env python
import threading
import sys
class HelloThread( threading.Thread ):
def hello( self, t ):
print 'hello from %(id)s for the %(t)dth time' \
% { 'id': self.ident, 't': t }
def run( self ):
for i in range(10):
self.hello( i )
if __name__ == '__main__':
t = list()
for i in range(5):
t.append( HelloThread() )
t[i].start()
for i in range(5):
t[i].join()
sys.exit(0)这里启动了 5 个线程,每个都数数,结果大致为hello from 4302057472 for the 0th time hello from 4302057472 for the 1th time hello from 4302057472 for the 2th time hello from 4302057472 for the 3th time hello from 4302594048 for the 0th time hello from 4302594048 for the 1th time hello from 4302594048 for the 2th time hello from 4302594048 for the 3th time hello from 4302594048 for the 4th time hello from 4302594048 for the 5th time hello from 4302594048 for the 6th time hello from 4302594048 for the 7th time hello from 4302594048 for the 8th time hello from 4302594048 for the 9th time hello from 4302594048 for the 0th time hello from 4302594048 for the 1th time hello from 4302594048 for the 2th time hello from 4302594048 for the 3th time hello from 4302594048 for the 4th time hello from 4302594048 for the 5th time hello from 4302594048 for the 6th time hello from 4302594048 for the 7th time hello from 4302594048 for the 8th time hello from 4302594048 for the 9th time hello from 4302057472 for the 4th time hello from 4302057472 for the 5th time hello from 4302057472 for the 6th time hello from 4302057472 for the 7th time hello from 4302057472 for the 8th time hello from 4302057472 for the 9th time hello from 4302594048 for the 0th time hello from 4302594048 for the 1th time hello from 4302594048 for the 2th time hello from 4302594048 for the 3th time hello from 4302594048 for the 4th time hello from 4302594048 for the 5th time hello from 4302594048 for the 6th time hello from 4302594048 for the 7th time hello from 4302594048 for the 8th time hello from 4302594048 for the 9th time hello from 4303130624 for the 0th time hello from 4303130624 for the 1th time hello from 4303130624 for the 2th time hello from 4303130624 for the 3th time hello from 4303130624 for the 4th time hello from 4303130624 for the 5th time hello from 4303130624 for the 6th time hello from 4303130624 for the 7th time hello from 4303130624 for the 8th time hello from 4303130624 for the 9th time某些输出被后面的覆盖了...
No comments:
Post a Comment