-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiprocess_print_in_order_main.py
More file actions
76 lines (55 loc) · 1.75 KB
/
multiprocess_print_in_order_main.py
File metadata and controls
76 lines (55 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import time
def my_task(x, task_id=None):
"""这是你的实际任务函数,可以任意复杂"""
if task_id is not None:
print(f"[任务{task_id}] my_task开始处理: {x}")
else:
print(f"my_task开始处理: {x}")
time.sleep(x * 0.1)
# 调用子函数
result1 = helper_function_a(x)
print(f" 子函数A返回: {result1}")
result2 = helper_function_b(result1)
print(f" 子函数B返回: {result2}")
final = result2 * 2
print(f"my_task完成,最终结果: {final}")
return final
def helper_function_a(y):
"""子函数A"""
print(f" [helper_a] 计算 {y} 的平方")
return y ** 2
def helper_function_b(z):
"""子函数B"""
print(f" [helper_b] 计算 {z} 加 100")
return z + 100
import sys
import os
# 导入模块
import multiprocess_print_in_order as mp_pio
def main():
"""主程序入口"""
# 测试数据
test_data = range(32)
print("🚀 主程序启动")
print("正在导入任务函数和多进程框架...")
# 运行并行任务
all_results = mp_pio.safe_parallel_ordered(my_task, test_data, num_workers=4)
# 展示结果
print("\n" + "=" * 60)
print("📊 任务执行结果汇总:")
print("=" * 60)
print(all_results)
# 测试数据
test_data = range(32,64)
print("🚀 主程序启动")
print("正在导入任务函数和多进程框架...")
# 运行并行任务
all_results = mp_pio.safe_parallel_ordered(my_task, test_data, num_workers=4)
# 展示结果
print("\n" + "=" * 60)
print("📊 任务执行结果汇总:")
print("=" * 60)
print(all_results)
if __name__ == "__main__":
# 这是关键:确保只在主模块中运行
main()