Python多线程

使用Python抓取线上所有权限信息

测试学习篇

1、入门例子_开启多个线程

#!/usr/bin/env python2.7
#-*- coding:utf-8 -*-

import threading
import time

class MyThread(threading.Thread):
    def run(self):
        global num
        time.sleep(1)

        num = num + 1
        msg = self.name + "set num=" + str(num)
        print msg

num = 0
def test():
    for i in range(5):
        t = MyThread()
        t.start()

if __name__ == '__main__':
    test()
[root@typhoeus79 ice_test_m threads]# ./test.py 
Thread-5set num=1
Thread-1set num=2
Thread-2set num=4
Thread-3set num=3
Thread-4set num=5

输出结果存在无序状态,结果不固定。

http://www.cnblogs.com/holbrook/archive/2012/03/04/2378947.html

2、Mutex锁

#!/usr/bin/env python2.7                                                                                                                                     
#-*- coding:utf-8 -*-

import threading
import time

class MyThread(threading.Thread):
    def run(self):
        global num 
        time.sleep(1)

        if mutex.acquire(1):
            num = num + 1 
            msg = self.name + "set num=" + str(num)

            print msg 
            mutex.release()

num = 0 
mutex = threading.Lock()
def test():
    for i in range(5):
        t = MyThread()
        t.start()

if __name__ == '__main__':
    test()

3、条件变量同步

http://www.cnblogs.com/holbrook/archive/2012/03/13/2394811.html

线程池

http://www.cnblogs.com/nsnow/archive/2010/04/18/1714596.html

posted @ 2014-01-14 17:42  小郭学路  阅读(153)  评论(0编辑  收藏  举报