python做中学(五)多线程的用法

系统 1507 0

多线程类似于同时执行多个不同程序,多线程运行有如下优点:

  • 使用线程可以把占据长时间的程序中的任务放到后台去处理。
  • 用户界面可以更加吸引人,比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进度条来显示处理的进度。
  • 程序的运行速度可能加快。
  • 在一些等待的任务实现上如用户输入、文件读写和网络收发数据等,线程就比较有用了。在这种情况下我们可以释放一些珍贵的资源如内存占用等等。

每个独立的线程有一个程序运行的入口、顺序执行序列和程序的出口。但是线程不能够独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制。

每个线程都有他自己的一组CPU寄存器,称为线程的上下文,该上下文反映了线程上次运行该线程的CPU寄存器的状态。

指令指针和堆栈指针寄存器是线程上下文中两个最重要的寄存器,线程总是在进程得到上下文中运行的,这些地址都用于标志拥有线程的进程地址空间中的内存。

  • 线程可以被抢占(中断)。
  • 在其他线程正在运行时,线程可以暂时搁置(也称为睡眠) -- 这就是线程的退让。

线程可以分为:

  • 内核线程: 由操作系统内核创建和撤销。
  • 用户线程: 不需要内核支持而在用户程序中实现的线程。

Python3 线程中常用的两个模块为:

  • _thread
  • threading(推荐使用)

thread 模块已被废弃。用户可以使用 threading 模块代替。所以,在 Python3 中不能再使用"thread" 模块。为了兼容性,Python3 将 thread 重命名为 "_thread"。

threading 模块除了包含 _thread 模块中的所有方法外,还提供的其他方法:

  • threading.currentThread(): 返回当前的线程变量。
  • threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
  • threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。

除了使用方法外,线程模块同样提供了Thread类来处理线程,Thread类提供了以下方法:

  • run():  用以表示线程活动的方法。
  • start(): 启动线程活动。

     

  • join([time]):  等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。
  • isAlive():  返回线程是否活动的。
  • getName():  返回线程名。
  • setName():  设置线程名。

举个例子:

            
               1
            
            
              #
            
            
              !/usr/bin/python3
            
            
               2
            
            
              #
            
            
               3birds coypright
            
            
               3
            
            
               4
            
            
              import
            
            
               threading

            
            
               5
            
            
              import
            
            
               time

            
            
               6
            
            
               7
            
             exitFlag =
            
               0

            
            
               8
            
            
               9
            
            
              class
            
            
               myThread (threading.Thread):

            
            
              10
            
            
              def
            
            
              __init__
            
            
              (self, threadID, name, counter):

            
            
              11
            
                     threading.Thread.
            
              __init__
            
            
              (self)

            
            
              12
            
                     self.threadIN =
            
               threadID

            
            
              13
            
                     self.name =
            
               name

            
            
              14
            
                     self.counter =
            
               counter

            
            
              15
            
            
              def
            
            
               run(self):

            
            
              16
            
            
              print
            
             (
            
              "
            
            
              3birds start thread:
            
            
              "
            
             +
            
               self.name)

            
            
              17
            
            
                      threadLock.acquire()

            
            
              18
            
                     print_time(self.name, self.counter, 5
            
              )

            
            
              19
            
            
                      threadLock.release()

            
            
              20
            
            
              print
            
             (
            
              "
            
            
              3birds exit thread:
            
            
              "
            
             +
            
               self.name)

            
            
              21
            
            
              22
            
            
              def
            
            
               print_time(threadName, delay, counter):

            
            
              23
            
            
              while
            
            
               counter:

            
            
              24
            
            
              if
            
            
               exitFlag:

            
            
              25
            
            
                          threadName.exit()

            
            
              26
            
            
                      time.sleep(delay)

            
            
              27
            
            
              print
            
             (
            
              "
            
            
              %s: %s
            
            
              "
            
             %
            
               (threadName, time.ctime(time.time())))

            
            
              28
            
                     counter -= 1

            
              29
            
            
              30
            
             threadLock =
            
               threading.Lock()

            
            
              31
            
             threads =
            
               []

            
            
              32
            
            
              33
            
             thread1 = myThread(1, 
            
              "
            
            
              Thread-1
            
            
              "
            
            , 1
            
              )

            
            
              34
            
             thread2 = myThread(2, 
            
              "
            
            
              Thread-2
            
            
              "
            
            , 2
            
              )

            
            
              35
            
            
              36
            
            
              #
            
            
               start new thread
            
            
              37
            
            
              thread1.start()

            
            
              38
            
            
              thread2.start()

            
            
              39
            
            
              thread1.join()

            
            
              40
            
            
              thread2.join()

            
            
              41
            
            
              print
            
            (
            
              "
            
            
              end the thread
            
            
              "
            
            )
          

  运行结果:

            3birds start thread:Thread-1
            
              
3birds start thread:Thread
            
            -2
            
              
Thread
            
            -1: Tue Jul 23 09:08:46 2019
            
              
Thread
            
            -1: Tue Jul 23 09:08:47 2019
            
              
Thread
            
            -1: Tue Jul 23 09:08:48 2019
            
              
Thread
            
            -1: Tue Jul 23 09:08:49 2019
            
              
Thread
            
            -1: Tue Jul 23 09:08:50 2019
            
              
3birds exit thread:Thread
            
            -1
            
              
Thread
            
            -2: Tue Jul 23 09:08:52 2019
            
              
Thread
            
            -2: Tue Jul 23 09:08:54 2019
            
              
Thread
            
            -2: Tue Jul 23 09:08:56 2019
            
              
Thread
            
            -2: Tue Jul 23 09:08:58 2019
            
              
Thread
            
            -2: Tue Jul 23 09:09:00 2019
            
              
3birds exit thread:Thread
            
            -2
            
              
end the thread
            
          

  参考文档:

1 http://www.runoob.com/python3/python3-multithreading.html


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论