多线程和多进程对比看看
多线程
import threading import time lock = threading.Lock() i = 0 def run(): global i while True: with lock: print(threading.current_thread().name, '=>', i, '\n') i = i + 1 time.sleep(2) threading.Thread(target=run, name='主').start() threading.Thread(target=run, name='次').start() print('done \n')
多进程1,
import multiprocessing import os import time def run(i, lock): try: while True: with lock: print(f"Process {multiprocessing.current_process().name}, PID: {os.getpid()} => {i.value}") i.value = i.value + 1 time.sleep(2) except Exception as e: print(f"Process {multiprocessing.current_process().name} error: {e}") if __name__ == '__main__': # 创建管理器进程 with multiprocessing.Manager() as manager: i = manager.Value('i', 0) # 共享整数变量 lock = multiprocessing.Lock() # 进程间锁 # 创建进程列表 processes = [] # 创建并启动进程 for name in ['主进程', '次进程']: p = multiprocessing.Process(target=run, args=(i, lock), name=name) p.start() processes.append(p) print('主进程已启动所有子进程\n') try: # 等待所有进程完成 (实际上它们会一直运行) for p in processes: p.join() except KeyboardInterrupt: print("\n接收到中断信号,终止所有进程...") for p in processes: p.terminate() for p in processes: p.join() finally: print("所有进程已终止")
2
import multiprocessing import os import time def run(i, lock): try: while True: with lock: print(f"Process {multiprocessing.current_process().name}, PID: {os.getpid()} => {i.value}") i.value = i.value + 1 time.sleep(2) except Exception as e: print(f"Process {multiprocessing.current_process().name} error: {e}") if __name__ == '__main__': i = multiprocessing.Value('i', 0) # 共享整数变量 lock = multiprocessing.Lock() # 进程间锁 multiprocessing.Process(target=run, args=(i, lock), name='主进程').start() multiprocessing.Process(target=run, args=(i, lock), name='次进程').start() print('done \n')
多线程,可以没有 __main__
多进程里的共享变量的2种方式
with multiprocessing.Manager() as manager: i = manager.Value('i', 0) # 共享整数变量
i = multiprocessing.Value('i', 0) # 共享整数变量
线程超时控制的例子:
import threading import time import ctypes import inspect def target_function(): """这是要在线程中执行的函数""" print("线程开始执行") try: # 模拟长时间运行的任务 for i in range(10): print(f"正在处理 {i}...") time.sleep(1) print("线程正常完成") except: print("线程被中断") def async_raise(tid, exctype): """向线程抛出异常""" if not inspect.isclass(exctype): raise TypeError("只有异常类型可以被抛出") res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(exctype)) if res == 0: raise ValueError("无效的线程ID") elif res != 1: ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), None) raise SystemError("PyThreadState_SetAsyncExc失败") class StoppableThread(threading.Thread): """可停止的线程类""" def __init__(self, target=None, args=(), kwargs=None): super().__init__(target=target, args=args, kwargs=kwargs if kwargs else {}) self._running = True def run(self): """重写run方法""" try: if self._target: self._target(*self._args, **self._kwargs) finally: del self._target, self._args, self._kwargs def stop(self): """停止线程""" if self.is_alive(): # 通过抛出异常来终止线程 async_raise(self.ident, SystemExit) def monitor_thread(thread, timeout): """监控线程并在超时时终止它""" start_time = time.time() while thread.is_alive(): elapsed = time.time() - start_time if elapsed > timeout: print(f"线程运行超过 {timeout} 秒,正在终止...") thread.stop() thread.join() # 等待线程真正结束 return False time.sleep(0.1) # 避免过于频繁检查 return True if __name__ == "__main__": # 创建并启动线程 thread = StoppableThread(target=target_function) thread.start() # 设置超时时间(秒) timeout = 3 # 监控线程 completed_normally = monitor_thread(thread, timeout) if completed_normally: print("线程在超时前完成") else: print("线程因超时被终止")