python多线程编程最简单实例子demo,使用线程锁,同步线程共享资源

即使 CPU 是单核的,大多现代操作系统也有能力让多个任务“同时进行”,这些操作系统有任务(进程)调度功能。操作系统分配资源是以进程为单位的,执行是以线程为单位的。同一个进程可以有多个线程,这些进程共享进程的资源,操作系统可以调度它们协调工作,就像同时进行一样。

多线程编程,不多说,直接上例子:

#encoding=utf8

import threading
import time


def fun1(lock):
    # lock.acquire()
    for i in range(0, 10):
        time.sleep(1)
        print "+++++++++ fun1 running %d +++++++++" % i
    # lock.release()


def fun2(lock):
    # lock.acquire()
    for i in range(0, 10):
        time.sleep(1)
        print "--------- fun2 running %d --------" % i
    # lock.release()

if __name__ == "__main__":
    # lock = threading.Lock()
    t1 = threading.Thread(target=fun1,args=(lock,))
    t2 = threading.Thread(target=fun2,args=(lock,))
    t1.start()
    t2.start()
    t1.join() 
    t2.join()

执行之,发现终端打印出:

# python test.py 
# python test.py 
+++++++++ fun1 running 0 +++++++++
--------- fun2 running 0 --------
+++++++++ fun1 running 1 +++++++++
--------- fun2 running 1 --------
+++++++++ fun1 running 2 +++++++++
--------- fun2 running 2 --------
+++++++++ fun1 running 3 +++++++++
--------- fun2 running 3 --------
+++++++++ fun1 running 4 +++++++++
--------- fun2 running 4 --------
+++++++++ fun1 running 5 +++++++++
--------- fun2 running 5 --------
+++++++++ fun1 running 6 +++++++++
--------- fun2 running 6 --------
+++++++++ fun1 running 7 +++++++++
--------- fun2 running 7 --------
+++++++++ fun1 running 8 +++++++++
--------- fun2 running 8 --------
+++++++++ fun1 running 9 +++++++++
--------- fun2 running 9 --------

现在,将关于锁的语句取消注释

#encoding=utf8

import threading
import time


def fun1(lock):
    # lock.acquire()
    for i in range(0, 10):
        time.sleep(1)
        print "+++++++++ fun1 running %d +++++++++" % i
    # lock.release()


def fun2(lock):
    # lock.acquire()
    for i in range(0, 10):
        time.sleep(1)
        print "--------- fun2 running %d --------" % i
    # lock.release()

if __name__ == "__main__":
    lock = threading.Lock()
    t1 = threading.Thread(target=fun1,args=(lock,))
    t2 = threading.Thread(target=fun2,args=(lock,))
    t1.start()
    t2.start()
    t1.join() 
    t2.join()

发现执行效果如下:

# python test.py 
+++++++++ fun1 running 0 +++++++++
+++++++++ fun1 running 1 +++++++++
+++++++++ fun1 running 2 +++++++++
+++++++++ fun1 running 3 +++++++++
+++++++++ fun1 running 4 +++++++++
+++++++++ fun1 running 5 +++++++++
+++++++++ fun1 running 6 +++++++++
+++++++++ fun1 running 7 +++++++++
+++++++++ fun1 running 8 +++++++++
+++++++++ fun1 running 9 +++++++++
--------- fun2 running 0 --------
--------- fun2 running 1 --------
--------- fun2 running 2 --------
--------- fun2 running 3 --------
--------- fun2 running 4 --------
--------- fun2 running 5 --------
--------- fun2 running 6 --------
--------- fun2 running 7 --------
--------- fun2 running 8 --------
--------- fun2 running 9 --------

可以看出,锁可以保证一段代码同时只有一条线程执行。

阅读更多:   Python
添加新评论

icon_redface.gificon_idea.gificon_cool.gif2016kuk.gificon_mrgreen.gif2016shuai.gif2016tp.gif2016db.gif2016ch.gificon_razz.gif2016zj.gificon_sad.gificon_cry.gif2016zhh.gificon_question.gif2016jk.gif2016bs.gificon_lol.gif2016qiao.gificon_surprised.gif2016fendou.gif2016ll.gif