3

我有一个使用 concurrent.futures 模块运行多线程的 python 3.8 脚本,并且在 MacOS Catalina (Intel) 中运行良好。迁移到 MacOS Monterey(Apple Silicon)之后。由于使用单线程,python 代码运行了很长时间。我正在使用带有 x86_64 并在 Rosetta 2 下运行的 Anaconda 附带的 python。尝试了 python 3.9(来自 Anaconda)并得到了相同的结果。如果有人可以提供解决方案或解决方法,我将不胜感激。谢谢。

这是显示问题的测试代码。在旧机器上,它运行 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.')

更新:

我找到了根本原因。我没插电源。当显示器休眠或屏幕保护程序启动时,wifi 将在 5 分钟后断开连接。这是 MacOS 的新电源调优。这是解决方案。

https://www.techrepublic.com/article/change-your-macos-power-settings-to-prevent-disconnecting-from-vpnwi-fi-when-the-computer-is-locked/

4

1 回答 1

1

您的代码在 macOS Big Sur 上按预期运行,它已经是 Apple Silicon,带有本机 Python 3.9。

看起来罪魁祸首是 Rosetta 或 Monterey,而不是你的代码,但在 Rosetta 上使用 Swift 时,我确实看到了一些奇怪的错误。你考虑过这样看吗?

于 2021-10-29T08:38:00.273 回答