thread 学习笔记

thread 对外提供的常用方法有:

start_new_thread:创建新线程

e.g. 看下面的示例更容易理解thread的线程机制:

import time
import thread
from time import ctime

def timer(i, interval):
    while True:
        print "id:%s, time:%s \n"%(i, ctime())
        time.sleep(interval)


def test():
    for i in range(5):
        thread.start_new_thread(timer, (i, (i*2+1)))

if __name__ == '__main__':
    test()
    time.sleep(2)

allocate_lock:生成线程锁对象

get_ident:返回当前线程标识

acquire([waitflag]):加锁  

waitflag = 0 : 如果可以获取则无需等待。

waitflag = 1 : 无条件获取锁

release:释放锁

locked:检查锁状态

原文地址:https://www.cnblogs.com/bjdxy/p/3134131.html