zoukankan      html  css  js  c++  java
  • python做中学(五)多线程的用法

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

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

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

    每个线程都有他自己的一组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

      参考文档:

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

  • 相关阅读:
    leetcode-22 括号生成
    Mysql安装与使用(详细)
    java开发环境的搭建
    LeetCode 55. Jump Game
    LeetCode 31. Next Permutation
    2019.1.12国家开发银行(国开行)笔试【补录】+ 面试总结
    苹果电脑MAMP环境配置以及关于“星球大战”知识图谱的复现
    创新工场-Deecamp2019冬令营笔试A卷 AND 面试内容
    使用scp命令在mac本机和linux服务器之间传输数据
    python小技巧之多行缩进,获取程序运行时间,删除非空文件夹
  • 原文地址:https://www.cnblogs.com/dylancao/p/11229697.html
Copyright © 2011-2022 走看看