I have a python 3.8 script running multithreading with concurrent.futures module and works fine in MacOS Catalina (Intel). After migrated to MacOS Monterey (Apple Silicon). The python code runs for a long time due to using single thread. I'm using the python come with Anaconda which is x86_64 and running under Rosetta 2. Tried python 3.9 (from Anaconda) and got the same result. I will be appreciated if anyone can provide solution or workaround. Thanks.
Here is a testing code to show the problem. In old machine, it runs 2 rounds and completed in 10 seconds. In new machine, it runs 10 rounds and completed in 50 seconds.
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.')
Update:
I find the root cause. I didn't plug the power. The wifi will be disconnected after 5 mins when the display sleeps or screensaver starts. This is the new power tuning of MacOS. Here is the solution.