Python编程技巧:如何优雅地实现多线程应用
在编写高效的Python应用程序时,多线程是一个非常重要的方面。它可以提高程序的响应速度和性能。本文将向大家介绍如何使用Python优雅地实现多线程应用。
线程是操作系统分配处理器时间的最小单位。Python中的线程模块是_thread和threading,其中threading模块更常用。
线程创建
Python中创建线程主要有两种方法:继承Thread类和实现runnable接口。我们这里使用继承Thread类的方法来创建线程。
``` python
import threading
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("开始线程:" + self.name)
print_time(self.name, self.counter, 5)
print("退出线程:" + self.name)
def print_time(threadName, delay, counter):
while counter:
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()
thread1.join()
thread2.join()
print("退出主线程")
```
这里我们定义了myThread类继承Thread类,并实现了构造器和run()方法。构造器初始化线程ID、名称和计数器,run()方法是线程执行的逻辑。在run()方法中,我们调用了print_time()函数来模拟线程执行的任务。
线程同步
当多个线程共同访问一个共享资源时,可能会产生竞争条件,导致数据不安全或程序意外终止。因此,线程同步是非常关键的。
Python中的线程同步主要有以下几种方式:
1. Lock:使用lock.acquire()和lock.release()方法来设置临界区,确保同一时间只有一个线程可以访问共享资源。
``` python
import threading
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("开始线程:" + self.name)
# 获取锁,保证同一时间只有一个线程可以访问共享资源
threadLock.acquire()
print_time(self.name, self.counter, 5)
# 释放锁
threadLock.release()
print("退出线程:" + self.name)
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("退出主线程")
```
2. RLock:作为Lock的改进版,允许同一个线程多次获取锁,但也必须释放相同次数的锁,否则其他线程依旧无法获取锁。
``` python
import threading
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("开始线程:" + self.name)
# 获取锁,保证同一时间只有一个线程可以访问共享资源
threadLock.acquire()
print_time(self.name, self.counter, 5)
# 释放锁
threadLock.release()
print("退出线程:" + self.name)
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
threadLock = threading.RLock()
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("退出主线程")
```
3. Semaphore:设置最多允许多少个线程同时访问共享资源。
``` python
import threading
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("开始线程:" + self.name)
# 获取信号量
semaphore.acquire()
print_time(self.name, self.counter, 5)
# 释放信号量
semaphore.release()
print("退出线程:" + self.name)
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
semaphore = threading.Semaphore(2)
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("退出主线程")
```
4. Condition:通过设置条件变量,让线程等待或通知其他线程状态变化。
``` python
import threading
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("开始线程:" + self.name)
# 获取条件变量
with condition:
condition.wait()
print_time(self.name, self.counter, 5)
print("退出线程:" + self.name)
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
condition = threading.Condition()
threads = []
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
thread1.start()
thread2.start()
threads.append(thread1)
threads.append(thread2)
time.sleep(1)
with condition:
condition.notifyAll()
for t in threads:
t.join()
print("退出主线程")
```
线程池
线程池是一个线程队列,当有新任务进来时,就会安排一个空闲线程执行任务。Python中的ThreadPoolExecutor可以轻松创建线程池。
``` python
import concurrent.futures
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
if __name__ == '__main__':
executor = concurrent.futures.ThreadPoolExecutor(2)
futures = []
for i in range(2):
futures.append(executor.submit(print_time, "Thread-%d" % i, i+1, 5))
concurrent.futures.wait(futures)
print("退出主线程")
```
总结
本文介绍了Python中实现多线程应用的一些技巧,包括线程创建、线程同步和线程池。对于那些需要编写高效Python应用程序的开发者来说,这些技巧能够帮助他们更好地掌握Python编程。