python多线程官网html
多线程相似于同时执行多个不一样程序,多线程运行有以下优势:
一、使用线程能够把占据长时间的程序中的任务放到后台去处理。
二、用户界面能够更加吸引人,好比用户点击了一个按钮去触发某些事件的处理,能够弹出一个进度条来显示处理的进度。
三、程序的运行速度可能加快。
四、一些等待的任务实现上如用户输入、文件读写和网络收发数据等,多线程就有用了。能够释放一些珍贵的资源如内存占用等等。python
线程中经常使用的两个模块为:_thread、threading。thread 模块已被废弃。用户可使用 threading 模块代替。安全
经过直接从 threading.Thread 继承建立一个新的子类,并实例化后调用 start() 方法启动新线程,即它调用了线程的 run() 方法。网络
import threading import time #继承threading.Thread类 class feiGegeThread (threading.Thread): #线程ID,线程名称,延迟时间 def __init__(self, threadID, name, delay): threading.Thread.__init__(self); self.threadID = threadID; self.name = name; self.delay = delay; #重写run方法 def run(self): print ("begin thread:" + self.name); #调用外部函数 print_time(self.name, self.delay, 3); print ("end thread:" + self.name); #定义打印时间方法:线程名字、延迟时间、打印次数 def print_time(threadName, delay, counter): while counter: time.sleep(delay) print ("%s: %s" % (threadName, time.ctime(time.time()))); counter -= 1; # 建立新线程 thread1 = feiGegeThread(1, "Thread-1", 1); thread2 = feiGegeThread(2, "Thread-2", 2); # 开启新线程 thread1.start(); thread2.start(); #join方法能够防止主线程提早结束。 thread1.join(); thread2.join(); #结果 begin thread:Thread-1 begin thread:Thread-2 Thread-1: Sun Jun 16 10:23:48 2019 Thread-2: Sun Jun 16 10:23:49 2019 Thread-1: Sun Jun 16 10:23:49 2019 Thread-1: Sun Jun 16 10:23:50 2019 end thread:Thread-1 Thread-2: Sun Jun 16 10:23:51 2019 Thread-2: Sun Jun 16 10:23:53 2019 end thread:Thread-2
若是多个线程共同对某个数据修改,则可能出现不可预料的结果,为了保证数据的正确性,须要对多个线程进行同步。
使用 Thread 对象的 Lock 和 Rlock 能够实现简单的线程同步,这两个对象都有 acquire 方法和 release 方法,对于那些须要每次只容许一个线程操做的数据,能够将其操做放到 acquire 和 release 方法之间。多线程
import threading import time class myThread (threading.Thread): def __init__(self, threadID, name, delay): threading.Thread.__init__(self); self.threadID = threadID; self.name = name; self.delay = delay; def run(self): print ("begin thread: " + self.name); # 获取锁,用于线程同步 threadLock.acquire(); print_time(self.name, self.delay, 3); # 释放锁,开启下一个线程 threadLock.release(); def print_time(threadName, delay, counter): while counter: time.sleep(delay); print ("%s: %s" % (threadName, time.ctime(time.time()))); counter -= 1; #获取锁对象 threadLock = threading.Lock(); #盛放线程的列表 threads = []; # 建立新线程 thread1 = myThread(1, "Thread-1", 1); thread2 = myThread(2, "Thread-2", 2); # 开启新线程 thread1.start(); thread2.start(); # 添加线程到线程列表 threads.append(thread1); threads.append(thread2); # 等待全部线程完成,主线程才退出 for t in threads: t.join(); 结果: begin thread: Thread-1 begin thread: Thread-2 Thread-1: Sun Jun 16 10:38:27 2019 Thread-1: Sun Jun 16 10:38:28 2019 Thread-1: Sun Jun 16 10:38:29 2019 Thread-2: Sun Jun 16 10:38:31 2019 Thread-2: Sun Jun 16 10:38:33 2019 Thread-2: Sun Jun 16 10:38:35 2019
瞧,打印函数一段时间内只容许一个线程操做。app
Python 的 Queue 模块中提供了同步的、线程安全的队列类,包括先入先出的队列 Queue,后入先出的队列 LifoQueue,和优先级队列 PriorityQueue。
这些队列都实现了锁原语,可以在多线程中直接使用,可使用队列来实现线程间的同步。函数
import queue import threading import time exitFlag = 0 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 ("begin thread:" + self.name); process_data(self.name, self.q); print ("end thread:" + self.name); def process_data(threadName, q): while not exitFlag: queueLock.acquire(); if not workQueue.empty(): data = q.get(); queueLock.release(); print ("%s processing %s" % (threadName, data)); else: queueLock.release(); time.sleep(1); threadList = ["Thread-1", "Thread-2", "Thread-3"]; nameList = ["One", "Two", "Three", "Four", "Five"]; queueLock = threading.Lock(); workQueue = queue.Queue(10); threads = []; threadID = 1; # 建立新线程 for tName in threadList: thread = myThread(threadID, tName, workQueue); thread.start(); threads.append(thread); threadID += 1; # 填充队列 queueLock.acquire(); for word in nameList: workQueue.put(word); queueLock.release(); # 等待队列清空 while not workQueue.empty(): pass # 通知线程是时候退出 exitFlag = 1; # 等待全部线程完成 for t in threads: t.join(); #结果: begin thread:Thread-1 begin thread:Thread-2 begin thread:Thread-3 Thread-3 processing One Thread-2 processing Two Thread-3 processing Three Thread-1 processing Four Thread-2 processing Five end thread:Thread-3 end thread:Thread-1 end thread:Thread-2