|
| 1 | +# _*_ coding: utf-8 _*_ |
| 2 | + |
| 3 | +""" |
| 4 | +python_thread_multiprocee.py by xianhu |
| 5 | +""" |
| 6 | + |
| 7 | +import time |
| 8 | +import threading |
| 9 | +import multiprocessing |
| 10 | + |
| 11 | +# 定义全局变量Queue |
| 12 | +g_queue = multiprocessing.Queue() |
| 13 | +g_search_list = list(range(10000)) |
| 14 | + |
| 15 | + |
| 16 | +# 定义一个IO密集型任务:利用time.sleep() |
| 17 | +def task_io(task_id): |
| 18 | + print("IOTask[%s] start" % task_id) |
| 19 | + while not g_queue.empty(): |
| 20 | + time.sleep(1) |
| 21 | + try: |
| 22 | + data = g_queue.get(block=True, timeout=1) |
| 23 | + print("IOTask[%s] get data: %s" % (task_id, data)) |
| 24 | + except Exception as excep: |
| 25 | + print("IOTask[%s] error: %s" % (task_id, str(excep))) |
| 26 | + print("IOTask[%s] end" % task_id) |
| 27 | + return |
| 28 | + |
| 29 | + |
| 30 | +# 定义一个计算密集型任务:利用一些复杂加减乘除、列表查找等 |
| 31 | +def task_cpu(task_id): |
| 32 | + print("CPUTask[%s] start" % task_id) |
| 33 | + while not g_queue.empty(): |
| 34 | + count = 0 |
| 35 | + for i in range(10000): |
| 36 | + count += pow(3*2, 3*2) if i in g_search_list else 0 |
| 37 | + try: |
| 38 | + data = g_queue.get(block=True, timeout=1) |
| 39 | + print("CPUTask[%s] get data: %s" % (task_id, data)) |
| 40 | + except Exception as excep: |
| 41 | + print("CPUTask[%s] error: %s" % (task_id, str(excep))) |
| 42 | + print("CPUTask[%s] end" % task_id) |
| 43 | + return task_id |
| 44 | + |
| 45 | + |
| 46 | +def init_queue(): |
| 47 | + print("init g_queue start") |
| 48 | + while not g_queue.empty(): |
| 49 | + g_queue.get() |
| 50 | + for _index in range(10): |
| 51 | + g_queue.put(_index) |
| 52 | + print("init g_queue end") |
| 53 | + return |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == '__main__': |
| 57 | + print("cpu count:", multiprocessing.cpu_count(), "\n") |
| 58 | + |
| 59 | + print("========== 直接执行IO密集型任务 ==========") |
| 60 | + init_queue() |
| 61 | + time_0 = time.time() |
| 62 | + task_io(0) |
| 63 | + print("结束:", time.time() - time_0, "\n") |
| 64 | + |
| 65 | + print("========== 多线程执行IO密集型任务 ==========") |
| 66 | + init_queue() |
| 67 | + time_0 = time.time() |
| 68 | + thread_list = [threading.Thread(target=task_io, args=(i,)) for i in range(5)] |
| 69 | + for t in thread_list: |
| 70 | + t.start() |
| 71 | + for t in thread_list: |
| 72 | + if t.is_alive(): |
| 73 | + t.join() |
| 74 | + print("结束:", time.time() - time_0, "\n") |
| 75 | + |
| 76 | + print("========== 多进程执行IO密集型任务 ==========") |
| 77 | + init_queue() |
| 78 | + time_0 = time.time() |
| 79 | + process_list = [multiprocessing.Process(target=task_io, args=(i,)) for i in range(multiprocessing.cpu_count())] |
| 80 | + for p in process_list: |
| 81 | + p.start() |
| 82 | + for p in process_list: |
| 83 | + if p.is_alive(): |
| 84 | + p.join() |
| 85 | + print("结束:", time.time() - time_0, "\n") |
| 86 | + |
| 87 | + print("========== 直接执行CPU密集型任务 ==========") |
| 88 | + init_queue() |
| 89 | + time_0 = time.time() |
| 90 | + task_cpu(0) |
| 91 | + print("结束:", time.time() - time_0, "\n") |
| 92 | + |
| 93 | + print("========== 多线程执行CPU密集型任务 ==========") |
| 94 | + init_queue() |
| 95 | + time_0 = time.time() |
| 96 | + thread_list = [threading.Thread(target=task_cpu, args=(i,)) for i in range(5)] |
| 97 | + for t in thread_list: |
| 98 | + t.start() |
| 99 | + for t in thread_list: |
| 100 | + if t.is_alive(): |
| 101 | + t.join() |
| 102 | + print("结束:", time.time() - time_0, "\n") |
| 103 | + |
| 104 | + print("========== 多进程执行cpu密集型任务 ==========") |
| 105 | + init_queue() |
| 106 | + time_0 = time.time() |
| 107 | + process_list = [multiprocessing.Process(target=task_cpu, args=(i,)) for i in range(multiprocessing.cpu_count())] |
| 108 | + for p in process_list: |
| 109 | + p.start() |
| 110 | + for p in process_list: |
| 111 | + if p.is_alive(): |
| 112 | + p.join() |
| 113 | + print("结束:", time.time() - time_0, "\n") |
| 114 | + |
| 115 | + exit() |
0 commit comments