码迷,mamicode.com
首页 > 编程语言 > 详细

python多线程实践小结

时间:2014-12-11 00:08:39      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   使用   sp   for   

参考:http://www.cnblogs.com/tqsummer/archive/2011/01/25/1944771.html

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

import serial
#from threading import Thread
from 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 = 0
        while 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 = True

class 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 = 0
        while 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 = True

if __name__ == __main__:#this is 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

 

python多线程实践小结

标签:style   blog   http   io   ar   color   使用   sp   for   

原文地址:http://www.cnblogs.com/cj2014/p/4156483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!