Python multi-core

import threading

# 用于存储结果的列表
results = []

# 创建一个锁对象
lock = threading.Lock()

# 任务1 
def task1():
    # 执行任务...
    result = "Task 1 completed"
  
    # 使用锁来保护对共享数据的访问
    with lock:
        results.append(result)
  
# 任务2
def task2():
    # 执行任务...
    result = "Task 2 completed"
    
    # 使用锁来保护对共享数据的访问
    with lock:
        results.append(result)
 
# 创建线程并启动
thread1 = threading.Thread(target=task1)
thread2 = threading.Thread(target=task2)
thread1.start()
thread2.start()
  
# 等待线程执行完毕
thread1.join()
thread2.join()
  
# 将结果写入文件
filename = "result.txt"
with open(filename, "w") as file:
    for i, result in enumerate(results):
        file.write("Task {} result: {}\n".format(i+1, result))