关于Python的进程线程协程之threading模块(二)Lock,RLock对象以及Semaphore,BoundedSemaphore对象

关于Python的进程线程协程之threading模块(二)Lock,RLock对象以及Semaphore,BoundedSemaphore对象

线程锁:由于线程之间是随机调度的,并且每个线程在cpu上只执行指定数量的字节码(100),所以当多个线程同时修改同一条数据时可能会出现脏数据,为了规避这一情况,线程锁应运而生,同一时刻允许一个线程执行操作。

实例一:

无锁情况,在cpu性能不佳的时候容易产生脏数据

# _*_coding:utf-8_*_
import threading
from time import sleep, ctime


maxlink = 10
# A pool of thread maxlink
thread_pool = []
# A pool of thread that save instantiation threads   存放线程实例的线程池
count = 0
# A global var

def loop(index):
    """A function for sleep sometime s """
    print "start loop %s at: " % index, ctime()
    global count
    count += 1
    sleep(2)
    print "end loop %s at: " % index, ctime()

def Thread_Pool(*arg):
    """A function that create and save instantiation
     of threading to thread pool"""
    func, LN = arg
    for i in range(LN):
        t = threading.Thread(target=func, args=(i,))
        thread_pool.append(t)

def Thread_Start(arg):
    """A function that represents a thread of control."""
    for i in range(arg):
        thread_pool[i].start()
    for i in range(arg):
        thread_pool[i].join()

def main():
    """A function of main"""
    print "process start at: ".upper(), ctime()
    Thread_Pool(loop, maxlink)
    Thread_Start(maxlink)
    print "process end at: ".upper(), ctime(), "now count :", count


if __name__ == '__main__':
main()

运行结果:

PROCESS START AT:  Fri Apr 28 16:49:28 2017
start loop 0 at:  Fri Apr 28 16:49:28 2017
start loop 1 at:  Fri Apr 28 16:49:28 2017
start loop 2 at:  Fri Apr 28 16:49:28 2017
start loop 3 at:  Fri Apr 28 16:49:28 2017
start loop 4 at:  Fri Apr 28 16:49:28 2017
start loop 5 at:  Fri Apr 28 16:49:28 2017
start loop 6 at:  Fri Apr 28 16:49:28 2017
start loop 7 at:  Fri Apr 28 16:49:28 2017
start loop 8 at:  Fri Apr 28 16:49:28 2017
start loop 9 at:  Fri Apr 28 16:49:28 2017
end loop 0 at: end loop 2 at: end loop 1 at:    Fri Apr 28 16:49:30 2017Fri Apr 28 16:49:30 2017Fri Apr 28 16:49:30 2017


end loop 6 at: end loop 3 at: end loop 7 at: end loop 5 at: end loop 4 at: end loop 8 at: end loop 9 at:       Fri Apr 28 16:49:30 2017Fri Apr 28 16:49:30 2017Fri Apr 28 16:49:30 2017Fri Apr 28 16:49:30 2017Fri Apr 28 16:49:30 2017Fri Apr 28 16:49:30 2017Fri Apr 28 16:49:30 2017






PROCESS END AT:  Fri Apr 28 16:49:30 2017 now count : 7

实例二:

关于Lock方法

#_*_coding:utf-8_*_
import threading
from time import sleep, ctime


_maxlink = 10
# A pool of thread max-link
thread_pool = []
# A list for saving instantiations of thread
lock_pool = []
# A list for saving instantiation of Lock
count = 0
# A global var


def loop(*args):
    """ A function for sleep sometime """
    _index,_lock = args
    print "start loop %s at: " % _index, ctime()
    _lock.acquire(2)
    #获取锁
    global count
    count += 1
    _lock.release()
    #释放锁
    sleep(2)
    print "end loop %s at: " % _index, ctime()


def Thread_Pool(*arg):
    """A function that create and save instantiation
     of threading to thread pool"""
    _func, _LN ,_Lock= arg
    for i in range(_LN):
        t = threading.Thread(target=_func, args=(i,_Lock[i]))
        thread_pool.append(t)

def Lock_Pool(arg):
    """A function that create max-link instantiations of Lock"""
    for i in range(arg):
        _L = threading.Lock()
        #创建锁对象
        lock_pool.append(_L)
    return lock_pool

def Thread_Start(arg):
    """A function that represents a thread of control."""
    for i in range(arg):
        thread_pool[i].start()
    for i in range(arg):
        thread_pool[i].join()

def main():
    """ A function of main """
    print "process start at: ".upper(), ctime()
    Lock_Pool(_maxlink)
    Thread_Pool(loop, _maxlink,lock_pool)
    Thread_Start(_maxlink)
    print "process end at: ".upper(), ctime(),"now count :",count


if __name__ == '__main__':
main()

运行结果:

PROCESS START AT:  Fri Apr 28 16:50:10 2017
start loop 0 at:  Fri Apr 28 16:50:10 2017
start loop 1 at:  Fri Apr 28 16:50:10 2017
start loop 2 at:  Fri Apr 28 16:50:10 2017
start loop 3 at:  Fri Apr 28 16:50:10 2017
start loop 4 at:  Fri Apr 28 16:50:10 2017
start loop 5 at:  Fri Apr 28 16:50:10 2017
start loop 6 at:  Fri Apr 28 16:50:10 2017
start loop 7 at:  Fri Apr 28 16:50:10 2017
start loop 8 at:  Fri Apr 28 16:50:10 2017
start loop 9 at:  Fri Apr 28 16:50:10 2017
end loop 0 at:  Fri Apr 28 16:50:12 2017
end loop 2 at: end loop 3 at:  end loop 5 at: end loop 4 at:  Fri Apr 28 16:50:12 2017end loop 1 at:   Fri Apr 28 16:50:12 2017
 Fri Apr 28 16:50:12 2017Fri Apr 28 16:50:12 2017
Fri Apr 28 16:50:12 2017


end loop 7 at: end loop 8 at: end loop 6 at:  end loop 9 at:   Fri Apr 28 16:50:12 2017 Fri Apr 28 16:50:12 2017Fri Apr 28 16:50:12 2017
Fri Apr 28 16:50:12 2017


PROCESS END AT:  Fri Apr 28 16:50:12 2017 now count : 10

实例三:

RLock方法

#_*_coding:utf-8_*_
import threading
from time import sleep, ctime


_max_link = 10
# A pool of thread max-link
thread_pool = []
# A pool of thread that save instantiation threads   存放线程实例的线程池
count = 0
# A global var


def loop(*args):
    '''A function for sleep sometime s ,and make global count ++'''
    _index,_lock = args
    print "start loop %s at: " % _index, ctime()
    _lock.acquire()
    #获取递归锁
    global count
    count += 1
    _lock.release()
    #释放递归锁
    sleep(2)
    print "end loop %s at: " % _index, ctime()


def Thread_Pool(*arg):
    """A function that create instantiations of threading"""
    _func, _LN ,_Lock= arg
    for i in range(_LN):
        t = threading.Thread(target=_func, args=(i,_Lock))#可能不能直接传列表元素,只能传列表
        thread_pool.append(t)


def Thread_Start(arg):
    """A function that represents a thread of control.
      And by calling them instantiations ,that produced from 
      'threading.Thread', from list thread_pool ,
      And block the main thread  
    """
    for i in range(arg):
        thread_pool[i].start()
    for i in range(arg):
        thread_pool[i].join()


def main():
    '''A function of main'''
    _lock = threading.RLock()
    #创建递归锁对象
    print "process start at: ".upper(), ctime()
    Thread_Pool(loop, _max_link,_lock)
    Thread_Start(_max_link)
    print "process end at: ".upper(), ctime(),"now count :",count


if __name__ == '__main__':
    main()

运行结果:

PROCESS START AT:  Fri Apr 28 16:52:13 2017
start loop 0 at:  Fri Apr 28 16:52:13 2017
start loop 1 at:  Fri Apr 28 16:52:13 2017
start loop 2 at:  Fri Apr 28 16:52:13 2017
start loop 3 at:  Fri Apr 28 16:52:13 2017
start loop 4 at:  Fri Apr 28 16:52:13 2017
start loop 5 at:  Fri Apr 28 16:52:13 2017
start loop 6 at:  Fri Apr 28 16:52:13 2017
start loop 7 at:  Fri Apr 28 16:52:13 2017
start loop 8 at:  Fri Apr 28 16:52:13 2017
start loop 9 at:  Fri Apr 28 16:52:13 2017
end loop 0 at: end loop 1 at: end loop 2 at:    Fri Apr 28 16:52:15 2017Fri Apr 28 16:52:15 2017Fri Apr 28 16:52:15 2017


end loop 6 at: end loop 3 at: end loop 5 at: end loop 4 at:     Fri Apr 28 16:52:15 2017Fri Apr 28 16:52:15 2017Fri Apr 28 16:52:15 2017Fri Apr 28 16:52:15 2017



end loop 8 at: end loop 7 at:  end loop 9 at:  Fri Apr 28 16:52:15 2017 Fri Apr 28 16:52:15 2017
Fri Apr 28 16:52:15 2017

PROCESS END AT:  Fri Apr 28 16:52:15 2017 now count : 10

关于Semaphore方法和BoundedSemaphore方法

Semaphore,是一种带计数的线程同步机制,当调用release时,增加计算,当acquire时,减少计数,当计数为0时,自动阻塞,等待release被调用。而在Python中存在两种Semphore,一种就是纯粹的Semphore,还有一种就是BoundedSemaphore


Semphore: 在调用release()函数时,不会检查,增加的计数是否超过上限(没有上限,会一直上升)
BoundedSemaphore:在调用release()函数时,会检查,增加的计数是否超过上限,这样就保证了使用的计数

实例四:

关于Semaphore方法

#_*_coding:utf-8_*_
import threading
from time import sleep, ctime



_max_link = 100
# A pool of thread max-link
thread_pool = []
# A list for saving instantiations of thread
lock_pool = []
# A list for saving instantiation of Lock
count = 0
# A global var


def loop(*args):
    """A function for sleep sometime s ,and make global count ++"""
    _index,_lock_Semaphore = args
    print "start loop %s at: " % _index, ctime()
    _lock_Semaphore.acquire()
    sleep(1)
    global count
    count += 1
    _lock_Semaphore.release()
    sleep(2)
    print "end loop %s at: " % _index, ctime()


def Thread_Pool(*arg):
    """A function that create instantiations of threading"""
    _func, _LN ,_Lock= arg
    for i in range(_LN):
        t = threading.Thread(target=_func, args=(i,_Lock))
        thread_pool.append(t)


def Thread_Start(arg):
    """A function that represents a thread of control.
      And by calling them instantiations ,that produced from 
      'threading.Thread', from list thread_pool ,
      And block the main thread  
    """
    for i in range(arg):
        thread_pool[i].start()
    for i in range(arg):
        thread_pool[i].join()


def main():
    '''A function of main'''
    _Semaphore = threading.Semaphore(3)
    print "process start at: ".upper(), ctime()
    Thread_Pool(loop, _max_link,_Semaphore)
    Thread_Start(_max_link)
    print "process end at: ".upper(), ctime(),"now count :",count


if __name__ == '__main__':
main()

实例五:

BoundedSemaphore

#_*_coding:utf-8_*_
import threading
from time import sleep, ctime

_max_link = 100
# A pool of thread max-link
thread_pool = []
# A list for saving instantiations of thread
lock_pool = []
# A list for saving instantiation of Lock
count = 0
# A global var


def loop(*args):
    """A function for sleep sometime s ,and make global count ++"""
    _index,_lock_Semaphore = args
    print "start loop %s at: " % _index, ctime()
    _lock_Semaphore.acquire()
    sleep(1)
    global count
    count += 1
    _lock_Semaphore.release()
    sleep(2)
    print "end loop %s at: " % _index, ctime()


def Thread_Pool(*arg):
    """A function that create instantiations of threading"""
    _func, _LN ,_Lock= arg
    for i in range(_LN):
        t = threading.Thread(target=_func, args=(i,_Lock))
        thread_pool.append(t)


def Thread_Start(arg):
    """A function that represents a thread of control.
      And by calling them instantiations ,that produced from 
      'threading.Thread', from list thread_pool ,
      And block the main thread  
    """
    for i in range(arg):
        thread_pool[i].start()
    for i in range(arg):
        thread_pool[i].join()


def main():
    '''A function of main'''
    _Semaphore = threading.BoundedSemaphore(3)
    print "process start at: ".upper(), ctime()
    Thread_Pool(loop, _max_link,_Semaphore)
    Thread_Start(_max_link)
    print "process end at: ".upper(), ctime(),"now count :",count


if __name__ == '__main__':
    main()

你可能感兴趣的:(Python开发)