multiprocessing模块提供了Process类,它允许你在多进程环境中并行执行任务。每个进程都有自己的内存空间和Python解释器,因此它们可以独立地运行。
from multiprocessing import Process
def func(name):
print('Hello', name)
if __name__ == '__main__':
p1 = Process(target=func, args=('Alice',))
p2 = Process(target=func, args=('Bob',))
p1.start()
p2.start()
p1.join()
p2.join()
threading模块提供了Thread类,它允许你在多线程环境中并行执行任务。线程共享同一个进程的内存空间,因此它们之间的通信比进程要快。
from threading import Thread
def func(name):
print('Hello', name)
if __name__ == '__main__':
t1 = Thread(target=func, args=('Alice',))
t2 = Thread(target=func, args=('Bob',))
t1.start()
t2.start()
t1.join()
t2.join()
concurrent.futures模块提供了ThreadPoolExecutor类,它是一个线程池执行器,允许你在多线程环境中并行执行任务。与手动创建线程不同,ThreadPoolExecutor管理线程池,并在需要时分配任务。这使得代码更简洁,更容易管理。
from concurrent.futures import ThreadPoolExecutor
def func(name):
print('Hello', name)
if __name__ == '__main__':
with ThreadPoolExecutor(max_workers=2) as executor:
executor.submit(func, 'Alice')
executor.submit(func, 'Bob')
这三种方式各有优缺点。
import threading
def my_thread():
print("Thread is running...")
return
t = threading.Thread(target=my_thread)
t.start()
t.join()
import threading
def my_thread():
try:
print("Thread is running...")
# Some code that may raise an exception
except Exception as e:
print("Exception occurred:", e)
return
t = threading.Thread(target=my_thread)
t.start()
t.join()
import threading
import time
class MyThread(threading.Thread):
def __init__(self):
super().__init__()
self._stop_event = threading.Event()
def run(self):
while not self._stop_event.is_set():
print("Thread is running...")
time.sleep(1) # Pause for a while to demonstrate the stop event.
if self._stop_event.is_set():
break # Exit the loop if stop event is set.
# Continue with your thread's logic here...
def stop(self):
self._stop_event.set() # Set the stop event to true.
self.join() # Wait for the thread to finish.
print("Thread has been stopped.")
import os
import time
pid = os.getpid() # 获取当前进程的ID
time.sleep(5) # 让进程运行一段时间
os.kill(pid, 9) # 使用9号信号杀死进程
多线程在IO密集型的操作下似乎也没有很大的优势(也许IO操作的任务再繁重一些就能体现出优势),在CPU密集型的操作下明显地比单线程线性执行性能更差,但是对于网络请求这种忙等阻塞线程的操作,多线程的优势便非常显著了
多进程无论是在CPU密集型还是IO密集型以及网络请求密集型(经常发生线程阻塞的操作)中,都能体现出性能的优势。不过在类似网络请求密集型的操作上,与多线程相差无几,但却更占用CPU等资源,所以对于这种情况下,我们可以选择多线程来执行