MacOS Monterey/Apple Silicon下Python多线程运行失效问题求解
问题描述
我有一段使用concurrent.futures模块实现多线程的Python 3.8脚本,在搭载Intel芯片的MacOS Catalina系统上运行正常。迁移到搭载Apple Silicon芯片的MacOS Monterey系统后,代码仅调用单线程运行,耗时大幅增加。我使用的是Anaconda提供的x86_64架构Python版本,通过Rosetta 2运行,尝试Anaconda的Python 3.9版本后问题依旧。
以下是用于复现问题的测试代码:在旧设备上,代码分2轮运行,10秒即可完成;在新设备上,代码分10轮运行,需要50秒才能完成。
import concurrent.futures import time pstart = time.time() tasks = list(range(1,11)) def sleep_5s(task): time.sleep(5) print(f'Task {task} start at: {time.time()}') def sleep_together(tasks): with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: for i,task in zip(tasks, executor.map(sleep_5s, tasks)): pass sleep_together(tasks) print('Total run time', time.time()-pstart, 'seconds.')
问题根因与解决方案
经排查,问题根源为设备未接通电源时,MacOS的新电源调优策略会在显示器休眠或屏保启动5分钟后断开WiFi连接。
解决方案为调整MacOS电源设置,关闭设备锁定状态下自动断开网络的相关策略即可。
内容的提问来源于stack exchange,提问作者angle
相关产品推荐
相关产品推荐

