亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python 多线程编程精通:高效并发技术全解析

运行多线程类似同时运行多个程序,但优势在于:

线程有开始、执行序列、结束。指令指针跟踪当前运行位置。线程可被抢占(中断),也可临时挂起(睡眠/让出)让其他线程运行。

启动新线程

调用 thread 模块的 start_new_thread 方法:

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

该调用在 Linux 和 Windows 上快速高效创建线程。立即返回,子线程启动并用传入的 args 调用 function。函数返回则线程终止。args 为元组,无参数传空元组。kwargs 为可选关键字参数字典。

示例

#!/usr/bin/python
import thread
import time
def print_time(threadName, delay):
    count = 0
    while count < 5:
        time.sleep(delay)
        count += 1
        print("%s: %s" % (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: unable to start thread")
while 1:
    pass

输出:

Thread-1: Thu Jan 22 15:42:17 2009
Thread-1: Thu Jan 22 15:42:19 2009
Thread-2: Thu Jan 22 15:42:19 2009
Thread-1: Thu Jan 22 15:42:21 2009
Thread-2: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:25 2009
Thread-2: Thu Jan 22 15:42:27 2009
Thread-2: Thu Jan 22 15:42:31 2009
Thread-2: Thu Jan 22 15:42:35 2009

thread 模块虽高效但功能有限,Python 2.4 引入的 threading 模块提供更强大的高级支持。

threading 模块

threading 模块暴露 thread 所有方法并提供额外方法:

并提供 Thread 类实现线程,主要方法:

使用 threading 创建线程

  1. 定义 Thread 子类。
  2. 重写 __init__(self[, args]) 添加参数。
  3. 重写 run(self[, args]) 实现线程逻辑。

创建子类实例,调用 start() 启动(内部调用 run())。

示例

#!/usr/bin/python
import threading
import time
exitFlag = 0
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("Starting " + self.name)
        print_time(self.name, 5, self.counter)
        print("Exiting " + self.name)
def print_time(threadName, counter, delay):
    while counter:
        if exitFlag:
            threadName.exit()
        time.sleep(delay)
        print("%s: %s" % (threadName, time.ctime(time.time())))
        counter -= 1
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
thread1.start()
thread2.start()
print("Exiting Main Thread")

输出:

Starting Thread-1
Starting Thread-2
Exiting Main Thread
Thread-1: Thu Mar 21 09:10:03 2013
Thread-1: Thu Mar 21 09:10:04 2013
Thread-2: Thu Mar 21 09:10:04 2013
Thread-1: Thu Mar 21 09:10:05 2013
Thread-1: Thu Mar 21 09:10:06 2013
Thread-2: Thu Mar 21 09:10:06 2013
Thread-1: Thu Mar 21 09:10:07 2013
Exiting Thread-1
Thread-2: Thu Mar 21 09:10:08 2013
Thread-2: Thu Mar 21 09:10:10 2013
Thread-2: Thu Mar 21 09:10:12 2013
Exiting Thread-2

线程同步

threading 模块提供简单锁机制同步线程。调用 Lock() 创建锁。

示例

#!/usr/bin/python
import threading
import time
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("Starting " + self.name)
        threadLock.acquire()
        print_time(self.name, self.counter, 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()
print("Exiting Main Thread")

输出:

Starting Thread-1
Starting Thread-2
Thread-1: Thu Mar 21 09:11:28 2013
Thread-1: Thu Mar 21 09:11:29 2013
Thread-1: Thu Mar 21 09:11:30 2013
Thread-2: Thu Mar 21 09:11:32 2013
Thread-2: Thu Mar 21 09:11:34 2013
Thread-2: Thu Mar 21 09:11:36 2013
Exiting Main Thread

多线程优先队列

Queue 模块创建可容纳指定数量项目的队列对象。控制方法:

示例

#!/usr/bin/python
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("Starting " + self.name)
        process_data(self.name, self.q)
        print("Exiting " + 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()
print("Exiting Main Thread")

输出:

Starting Thread-1
Starting Thread-2
Starting Thread-3
Thread-1 processing One
Thread-2 processing Two
Thread-3 processing Three
Thread-1 processing Four
Thread-2 processing Five
Exiting Thread-3
Exiting Thread-1
Exiting Thread-2
Exiting Main Thread

Python

  1. 精通Fanuc G76螺纹循环:CNC机床操作员完全指南
  2. 挤压攻丝与滚压攻丝:核心差异深度解析
  3. 精通Python数据类型:实战指南
  4. 精通Python运算符:全面指南
  5. 精通Python While循环:语法、示例与最佳实践
  6. 精通Python Pass语句:实战指南
  7. 精通Python函数:语法、类型与实战示例
  8. 精通Python函数参数:位置、关键字与默认参数
  9. 精通 Python 字典:创建、操作与进阶技巧
  10. 精通 Python 面向对象编程:类、继承、封装与多态
  11. Python 网络编程:掌握 Socket 与高级协议
  12. Python C 扩展开发指南:构建高性能原生模块