fragment/python进程池.py
2021-08-29 00:02:47 +08:00

65 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python2
# -*- coding:utf-8 -*-
#=========================================
# Filename : process_pool.py
# Author : Colben
# Create : 2018-06-25 16:09
#=========================================
from multiprocessing import Process
# 进程池容量,即同时运行的最多任务数量
POOL_SIZE = 10
# 初始化任务列表这里用字典存储key是任务value是子进程
PROCESS_LIST = {}
# 执行一个任务
def exec_task():
#print task, '\033[32mbegan ...\033[0m'
pass
# 检查任务列表情况
# empty = False 用于查看列表是否已满达到10个)
# empty = True 用于查看列表是否已空(0个)
def check_pool_state(empty=False):
while True:
print "\033[2J\033[0;0H"
for task, process in PROCESS_LIST.items():
if process.is_alive():
print 'Handling', task, '...'
else:
#print task, '\033[32mfinished\033[0m'
PROCESS_LIST.pop(task)
if not empty and POOL_SIZE > len(PROCESS_LIST): break
if empty and 0 == len(PROCESS_LIST):break
sleep(0.5)
return
# 批量启动任务
def start_process_pool(tasks):
for task in tasks:
sub_process = Process(target=exec_task, args=(task,))
# 启动子进程
sub_process.start()
# 记录子进程信息到任务列表
PROCESS_LIST[task] = sub_process
# 查看列表是否已满
check_pool_state()
# 查看列表是否已空
check_pool_state(empty=True)
return
# 测试开始 --------
def main():
tasks = [6,7,8,9]
# 重写上面的 exec_task 函数,实现阶乘
start_process_pool(tasks)
return
if '__main__' == __name__:
main()
# 测试结束 --------