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

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

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

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

内核线程:由操作系统内核创建和撤销。
用户线程:不需要内核支持而在用户程序中实现的线程。
Python3 线程中常用的两个模块为:

_thread
threading(推荐使用)
函数式
调用 _thread 模块中的start_new_thread()函数来产生新线程。语法如下:

_thread.start_new_thread ( function, args[, kwargs] )

参数说明:

function - 线程函数。
args - 传递给线程函数的参数,他必须是个tuple类型。
kwargs - 可选参数。

import _thread, time

定义线程函数

def print_time(threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
# 返回当前时间的时间戳(1970纪元后经过的浮点秒数), 并格式化输出
print("{}: {}".format(threadName, time.ctime(time.time()) ))
try:
_thread.start_new_thread( print_time, (“Thread-1”, 2))
_thread.start_new_thread( print_time, (“Thread-2”, 4))
except:
print(“Error”)

while 1:

让线程有足够的时间完成

pass

E:\PyPro>python thread.py
Thread-1: Thu Apr 12 09:01:56 2018
Thread-2: Thu Apr 12 09:01:58 2018
Thread-1: Thu Apr 12 09:01:58 2018
Thread-1: Thu Apr 12 09:02:00 2018
Thread-2: Thu Apr 12 09:02:02 2018
Thread-1: Thu Apr 12 09:02:02 2018
Thread-1: Thu Apr 12 09:02:05 2018
Thread-2: Thu Apr 12 09:02:06 2018
Thread-2: Thu Apr 12 09:02:10 2018
Thread-2: Thu Apr 12 09:02:14 2018

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

threading.currentThread(): 返回当前的线程变量。
threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
线程模块同样提供了Thread类来处理线程,Thread类提供了以下方法:

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

import threading, time

创建进程类

class myThread(threading.Thread):

构造函数

def init(self, threadID, name, counter):
threading.Thread.init(self)
self.threadID = threadID
self.name = name
self.counter = counter

重写run()

def run(self):
print(“Thread Strat:” + self.name)
print_time(self.name, self.counter, 5)
print(“Thread Exit:” + self.name)

def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print("{}: {}".format(threadName, time.ctime(time.time()) ))
counter -= 1

创建线程

thread1 = myThread(1001, “Thread-1”, 1)
thread2 = myThread(1002, “Thread-2”, 2)

开启线程

print("Thread-1 is Alive? ", thread1.isAlive())
thread1.start()
thread2.start()
print("Thread-1 is Alive? ", thread1.isAlive())
thread1.join()
thread2.join()
print("Thread-1 is Alive? ", thread1.isAlive())
print(“exit”)

E:\PyPro>python threadClass.py
Thread-1 is Alive? False
Thread Strat:Thread-1
Thread Strat:Thread-2
Thread-1 is Alive? True
Thread-1: Thu Apr 12 10:15:53 2018
Thread-1: Thu Apr 12 10:15:54 2018
Thread-2: Thu Apr 12 10:15:54 2018
Thread-1: Thu Apr 12 10:15:55 2018
Thread-1: Thu Apr 12 10:15:56 2018
Thread-2: Thu Apr 12 10:15:56 2018
Thread-1: Thu Apr 12 10:15:57 2018
Thread Exit:Thread-1
Thread-2: Thu Apr 12 10:15:58 2018
Thread-2: Thu Apr 12 10:16:00 2018
Thread-2: Thu Apr 12 10:16:02 2018
Thread Exit:Thread-2
Thread-1 is Alive? False
exit

不难发现,线程是通过start()函数激活,而不是对象建立时激活的!

线程同步
多线程的优势在于可以同时运行多个任务(至少感觉起来是这样)。但是当线程需要共享数据时,可能存在数据不同步的问题。

使用 Thread 对象的 Lock 和 Rlock 可以实现简单的线程同步,这两个对象都有 acquire 方法和 release 方法,对于那些需要每次只允许一个线程操作的数据,可以将其操作放到 acquire 和 release 方法之间。

import threading, time

创建锁

threadLock = threading.Lock()

创建线程列表

threads = []

class myThread(threading.Thread):
def init(self, threadID, name, counter):
threading.Thread.init(self)
self.threadID = threadID
self.name = name
self.counter = counter

def run(self):
print("Thread Start: " + self.name)
# 获取锁,同步线程
threadLock.acquire()
print_time(self.name, self.counter, 3)
# 释放锁,开启下一个线程
threadLock.release()
print("Thread Exit: " + self.name)

def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print("{}: {}".format(threadName, time.ctime()))
counter -= 1

创建线程

thread1 = myThread(1001, “Thread-1”, 1)
thread2 = myThread(1002, “Thread-2”, 2)

开启线程

thread1.start()
thread2.start()

添加线程列表

threads.append(thread1)
threads.append(thread2)

等待所有线程完成

for t in threads:
t.join()
print(“exit”)

E:\PyPro>python synchronize.py
Thread Start: Thread-1
Thread Start: Thread-2
Thread-1: Thu Apr 12 11:00:49 2018
Thread-1: Thu Apr 12 11:00:50 2018
Thread-1: Thu Apr 12 11:00:51 2018
Thread Exit: Thread-1
Thread-2: Thu Apr 12 11:00:53 2018
Thread-2: Thu Apr 12 11:00:55 2018
Thread-2: Thu Apr 12 11:00:57 2018
Thread Exit: Thread-2
exit

线程优先级队列
Python 的 Queue 模块中提供了同步的、线程安全的队列类,包括FIFO队列Queue,LIFO队列LifoQueue,和优先级队列 PriorityQueue。

这些队列都实现了锁原语,能够在多线程中直接使用,可以使用队列来实现线程间的同步。

Queue 模块中的常用方法:

Queue.qsize() 返回队列的大小
Queue.empty() 如果队列为空,返回True,反之False
Queue.full() 如果队列满了,返回True,反之False
Queue.full 与 maxsize 大小对应
Queue.get([block[, timeout]])获取队列,timeout等待时间
Queue.get_nowait() 相当Queue.get(False)
Queue.put(item) 写入队列,timeout等待时间
Queue.put_nowait(item) 相当Queue.put(item, False)
Queue.task_done() 在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号
Queue.join() 实际上意味着等到队列为空,再执行别的操作

import queue, threading, time

exitFlag = 0

创建锁

queueLock = threading.Lock()

创建队列

workQueue = queue.Queue(10)

class myThread(threading.Thread):
def init(self, threadID, name, q):
threading.Thread.init(self)
self.threadID = threadID
self.name = name
self.q = q

def run(self):
print("Thread Start: " + self.name)
process_data(self.name, self.q)
print("Thread Exit: " + self.name)

def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
print("{} processing {}".format(threadName, data))
else:
queueLock.release()
time.sleep(1)

threadList = [“Thread-1”, “Thread-2”, “Thread-3”]
nameList = [“One”, “Two”, “Three”, “Four”, “Five”]
threads = []
threadID = 1

创建新线程

for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1

填充队列

queueLock.acquire()
print(“队列填充中>>>>>>>>>>>>>>”)
time.sleep(1)
for word in nameList:
workQueue.put(word)
print(“队列填充完毕>>>>>>>>>>>>>>”)
queueLock.release()

等待队列清空

while not workQueue.empty():
pass

通知线程退出

exitFlag = 1

等待所有线程完成

for t in threads:
t.join()
print(“exit”)

E:\PyPro>python queueue.py
Thread Start: Thread-1
Thread Start: Thread-2
Thread Start: Thread-3
队列填充中>>>>>>>>>>>>>>
队列填充完毕>>>>>>>>>>>>>>
Thread-3 processing One
Thread-1 processing Two
Thread-2 processing Three
Thread-3 processing Four
Thread-1 processing Five
Thread Exit: Thread-2
Thread Exit: Thread-1
Thread Exit: Thread-3
exit

源码中其实实现了三个进程读取同一个队列,按照先进先出原则实现锁定。

用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码。通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行run()方法,这里方法 run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程随即终止。

join的作用是保证当前线程执行完成后,再执行其它线程。join可以有timeout参数,表示阻塞其它线程timeout秒后,不再阻塞。。一般线程的start()之后,所有操作结束后都要进行thread.join()。确保语句的输出是join()后面的程序是等线程结束后再执行的。

作者:Eappo_Geng
来源:https://my.oschina.net/gain/blog/1794659

十分钟带你了解 Python3 多线程核心知识相关推荐

  1. 十分钟带你理解Kubernetes核心概念

    原文地址:http://www.dockone.io/article/932 十分钟带你理解Kubernetes核心概念 本文将会简单介绍Kubernetes的核心概念.因为这些定义可以在Kubern ...

  2. 十分钟带你打造unity3d第一人称射击

    那抱歉,上次那个Unity+kinect还没有更新,最近在深一步研究,不久将更新(绝对不负众望)!现在进入正题,十分钟带你打造unity3D第一人称射击游戏! 看完本篇博客,你将学会第一人称控制,粒子 ...

  3. 这是我见过最好的Python教程:十分钟带你认识Python

    这篇文章主要介绍了简洁的十分钟Python入门教程,Python语言本身的简洁也使得网络上各种Python快门入门教程有着很高的人气,本文是国内此类其中的一篇,需要的朋友可以参考下 [简介] Pyth ...

  4. 简单易懂!十分钟带你了解:统计学到底是干什么的

    CDA数据分析师 出品 编译:Mika [导读] 统计学在我们的日常生活中无处不在,它有助于我们更好地了解世界,并做出更好的决策. 在今天的内容里,我们将带你用十分钟了解:统计学到底是关于什么的一门学 ...

  5. 深入理解 Java 多线程核心知识:跳槽面试必备

    作为一个 Java 开发人员,多线程是一个逃不掉的话题,不管是工作还是面试,但理解起来比较模糊难懂,因为多线程程序在跑起来的时候比较难于观察和跟踪.搞懂多线程并发知识,可以在面试的时候和周围人拉开差距 ...

  6. [k8s] 第一章 十分钟带你理解Kubernetes核心概念

    本章节主要介绍应用程序在服务器上部署方式演变以及kubernetes的概念.组件和工作原理. 应用部署方式演变 在部署应用程序的方式上,主要经历了三个时代: 传统部署:互联网早期,会直接将应用程序部署 ...

  7. 给我十分钟带你过完java多线程所有基础知识

    目录: 1.并发并行与线程进程 2.认识CPU和主线程 3.多线程的三种创建方式 4.三种创建多线程方式的进一步探究和对比 5.匿名内部类的多线程创建 6.多线程内存的分析 7.深度了解线程run() ...

  8. Java集合核心详解【十分钟带你了解整个集合体系】

    前言: 集合是Java中非常重要的一章,学习难度也相对较大,不会很快就能掌握,这里我们先对集合框架有一个大概的了解,记住其中的基础知识,后面深入研究某一个集合时,才能更好的掌握. 文章目录 一.集合介 ...

  9. 移动端视频开发通过什么方式实现直播?十分钟带你快速了解

    原文链接:https://www.jianshu.com/p/92b71382724f 本专栏专注分享大型Bat面试知识,后续会持续更新,喜欢的话麻烦点击一个关注 现在的移动端的音视频这些在近段时间非 ...

最新文章

  1. python面向对象编程 -- 封装、继承
  2. (原创)用c++11打造好用的any
  3. 大连大学计算机科学与技术考研真题,2016年大连大学计算机科学与技术数据库系统原理复试笔试最后押题五套卷...
  4. FFMPEG结构体分析之AVCodec
  5. JavaScript类型转换的有趣应用
  6. VC读取PE文件的OEP
  7. Vue.js(学习Vue3之前必须要掌握的知识)
  8. 华为手机左侧快捷方式,手机桌面太复杂?华为手机自动对齐整理桌面图标方法!...
  9. img图片在父元素中居中的方法
  10. 互融云人行二代征信系统对接服务
  11. Day2-开发环境搭建——百问网7天物联网智能家居
  12. 全键盘 linux 手机,当年青葱的岁月:10佳全键盘智能手机盘点
  13. 鸿蒙os2.0第一批升级名单,首批升级鸿蒙OS名单终于来了!
  14. 数据爬取遇到EventStream是个什么东西?EventSource与websocket有何区别?Java后台如何获取爬取数据并入库?EventStream后台服务怎么写?
  15. 安装了 PowerBuilder 10 Enterprise 玩玩...
  16. vue swiper中点击预览图片 全屏预览图片 vue点击查看大图
  17. springboot+Quartz整合!!!简单实用
  18. libcef-简单介绍-快速链接-源代码发布
  19. CC2640R2F学习笔记(24)——系统延时使用
  20. NPR技术(2)—Hatching 素描渲染

热门文章

  1. 电脑语音设置修复计算机,一键修复电脑没声音的解决办法
  2. Java复习题库new
  3. 游戏广告变现指南4-如何衡量广告变现效果
  4. ArcGIS教程:制作风或水流速流向图
  5. 基于遗传算法优化的lssvm回归预测matlab代码
  6. 我的服务器新手箱子无限,家庭影音之路 篇一:#原创新人#服务器安装Rutorrent(PT盒子)Seedbox教程...
  7. kdj超卖_kdj超卖是什么意思,kdi超卖的部分使用技巧
  8. unity3d 流光效果 shader控制 不用代码
  9. PDF翻译,仅支持英译中,可以下载翻译后的pdf或者word版
  10. PCB ODB++(Gerber)图形绘制实现方法