当前位置:首页 > Python教程 > python技巧

python多线程实践小结

#!/usr/bin/env python
import sys
import threading

import serial
#from threading import Threadfrom time import sleep

sub_msg_lock = threading.Lock()class thread1_test(threading.Thread):

    def __init__(self,para_for_thread1,name=wang_thread1):
        threading.Thread.__init__(self)
        self.thread1_num = para_for_thread1
        #self.thread1_stop_flag = False

    def run(self):#name must is:run
        thread1_count = 0while thread1_count < 10:#not self.thread1_stop_flag
            print("thread1 started")
            sleep(0.5)
            self.thread1_num = self.thread1_num + 1             print("thread1_num:",self.thread1_num)
            thread1_count += 1     #def thread1_stop(self):
        #self.thread1_stop_flag = Trueclass thread2_test(threading.Thread):

    def __init__(self,para_for_thread2,name=wang_thread2):  
        threading.Thread.__init__(self)
        self.thread2_num = para_for_thread2
        #self.thread2_stop_flag = False

    def run(self):
        thread2_count = 0while thread2_count < 10:#not self.thread1_stop_flag
            print("thread2 started")
            sleep(1)
            self.thread2_num = self.thread2_num + 1             print("thread2_num:",self.thread2_num)
            thread2_count += 1     def thread2_stop(self):
        #thread2_test_instance = thread2_test()
        #thread2_name = thread2_test_instance.getName()
        #wrong: getName() is method of thread,so must used by instance of thread        
        #self.thread2_stop_flag = Trueif __name__ == __main__:#thisis the main thread
    thread1 = thread1_test(0)
    thread2 = thread2_test(100)

    thread1.setDaemon(True)
    thread2.setDaemon(True)
    thread1.start()
    thread2.start()
    sleep(1)

    thread1_name = thread1.getName()
    print("thread1_name:",thread1_name)
    thread2_name = thread2.getName()
    print("thread2_name:",thread2_name)

    thread1_isdaemon = thread1.isDaemon()
    #default Flase: means the son thread will not died with main thread (son has freedom)
    #we can through setDaemon(True) to make son threads die with main thread (son can not longer than main) = (main died son must died,son can ealyer died than mian)
    print("thread1_isdaemon:",thread1_isdaemon)
    thread2_isdaemon = thread2.isDaemon()
    print("thread2_isdaemon:",thread2_isdaemon)

    #thread1.thread1_stop()
    #thread2.thread2_stop()

    thread1_isalive = thread1.isAlive()
    print("thread1_isalive:",thread1_isalive)
    thread2_isalive = thread2.isAlive()
    print("thread2_isalive:",thread2_isalive)

    #thread1.join()
    thread2.join(7)#main thread wait thread1 and thread2 then main thread go on
    #sleep(3)
    print ("mian thread terminate!")
    print ("All threads terminate!")
关于线程的方法:

    就我个人而言,比较喜欢第二种方式,即创建自己的线程类,必要时重写threading.Thread类的方法,线程的控制可以由自己定制。

threading.Thread类的使用:

1,在自己的线程类的__init__里调用threading.Thread.__init__(self, name = ‘threadname‘) 其中threadname为线程的名字

2, run(),通常需要重写,编写代码实现做需要的功能。

3,getName(),获得线程对象名称

4,setName(),设置线程对象名称

5,start(),启动线程

6,jion([timeout]),等待另一线程结束后再运行。不设置timeout则等待该线程结束,设置timeout后,最多等待线程timeout这么长时间,然后继续运行下面的程序.

7,setDaemon(bool),设置子线程是否随主线程一起结束,必须在start()之前调用。默认为False,即子线程自由与主线程.否则,主线程结束时不管子线程是否结束,都会结束子线程.

8,isDaemon(),判断线程是否随主线程一起结束。

9,isAlive(),检查线程是否在运行中。

    此外threading模块本身也提供了很多方法和其他的类,可以帮助我们更好的使用和管理线程。可以参看http://www.python.org/doc/2.5.2/lib/module-threading.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!