PHP前端开发

详解python中Threadpool线程池任务终止示例代码

百变鹏仔 3小时前 #Python
文章标签 示例

需求

加入我们需要处理一串个位数(0~9),奇数时需要循环打印它;偶数则等待对应时长并完成所有任务;0则是错误,但不需要终止任务,可以自定义一些处理。

关键点

定义func函数处理需求

callback处理返回结果,只有偶数和0返回;奇数会一直执行;要控制线程池状态,则需要针对偶数和0时抛出异常,并捕获异常处理。

threadpool定义线程池并发

实现

# -*- coding: utf-8 -*-from threadpool import makeRequests, ThreadPoolimport timefrom multiprocessing import Process

异常定义和特殊值(0)定义

class Finish(SyntaxWarning):    pass
class PauseInfo(SyntaxWarning):    passpause_num = 0

func函数定义

0时返回False,其他偶数返回True

立即学习“Python免费学习笔记(深入)”;

def func(para):    if para == pause_num:        print('start for %d and wait %ds' % (para, 4))        time.sleep(4)        print('error bcs ',para)        return False    if para % 2 == 0:        print('start for %d and wait %ds' % (para, para))        time.sleep(para)        print('stop for', para)        return True    while True:        print('continue for', para)        time.sleep(para)

callback定义

def callback(request, result):    if result:        raise Finish    else:        raise PauseInfo

线程池处理

Finish标识任务完成,再次诱发异常退出线程池处理;

def main_thread(paras):    pool = ThreadPool(10)    requests = makeRequests(callable_=func, args_list=paras, callback=callback)    [pool.putRequest(req) for req in requests]    while True:        try:            pool.wait()        except Finish as e:            raise SystemExit        except PauseInfo as e:            print('Pause bcs %d but will continue' % pause_num)        except Exception as e:            print('Unknown error so will quit')            raise SystemExit

主函数起一个测试进程

if name == 'main':    while True:        s = input('Input number list to test and any other word to quit')        paras = []        for para in s:            if para.isnumeric():                paras.append(int(para))            else:                break        try:            thread_test = Process(target=main_thread, args=(paras,))            thread_test.start()            thread_test.join(timeout=20)        except TimeoutError as e:            print('task timeout')        except Exception as e:            print('unknow error:',e)
 

结果验证

处理108,看打印可以看到,1被循环处理,0处理的时候报错;8处理完毕则任务结束

Input number list to test and any other word to quit
108
continue for 1
start for 0 and wait 4s
start for 8 and wait 8s
continue for 1
continue for 1
continue for 1
error bcs  0
continue for 1
Pause bcs 0 but will continue
continue for 1
continue for 1
continue for 1
continue for 1
stop for 8
Input number list to test and any other word to quit