摘要: 线程局部存储(tls),对于同一个local,线程无法访问其他线程设置的属性;线程设置的属性不会被其他线程设置的同名属性替换。代码: 1 import threading 2 3 local = threading.local() 4 local.tname = "main" 5 6 def func(info): 7 local.tname = info 8 print local.tname 9 10 t1 = threading.Thread(target=func, args=['funcA'])11 t2 = threading.Thread(ta 阅读全文
posted @ 2014-03-05 11:32 Fly Hawk 阅读(802) 评论(0) 推荐(0) 编辑
摘要: Timer: 隔一定时间调用一个函数,如果想实现每隔一段时间就调用一个函数的话,就要在Timer调用的函数中,再次设置Timer。Timer是Thread的一个派生类 1 import threading 2 import time 3 4 def hello(name): 5 print "hello %s\n" % name 6 7 global timer 8 timer = threading.Timer(2.0, hello, ["Hawk"]) 9 timer.start()10 11 if __name__ == "__main_ 阅读全文
posted @ 2014-03-05 11:26 Fly Hawk 阅读(55574) 评论(0) 推荐(0) 编辑
摘要: Event: 是线程同步的一种方式,类似于一个标志,当该标志为false时,所有等待该标志的线程阻塞,当为true时,所有等待该标志的线程被唤醒isSet(): 当内置标志为True时返回True。set(): 将标志设为True,并通知所有处于等待阻塞状态的线程恢复运行状态。clear(): 将标志设为False。wait([timeout]): 如果标志为True将立即返回,否则阻塞线程至等待阻塞状态,等待其他线程调用set() 1 import threading 2 import time 3 4 event = threading.Event() 5... 阅读全文
posted @ 2014-03-05 11:05 Fly Hawk 阅读(1000) 评论(0) 推荐(0) 编辑
摘要: Semphore,是一种带计数的线程同步机制,当调用release时,增加计算,当acquire时,减少计数,当计数为0时,自动阻塞,等待release被调用。而在Python中存在两种Semphore,一种就是纯粹的Semphore,还有一种就是BoundedSemaphore。区别:Semphore: 在调用release()函数时,不会检查,增加的计数是否超过上限(没有上限,会一直上升)BoundedSemaphore:在调用release()函数时,会检查,增加的计数是否超过上限,这样就保证了使用的计数代码: 1 import threading 2 import time 3 4 . 阅读全文
posted @ 2014-03-05 10:55 Fly Hawk 阅读(4014) 评论(0) 推荐(0) 编辑