zoukankan      html  css  js  c++  java
  • 多线程的异常处理

    1. 

    # -*- coding: utf-8 -*-def get_multithreading_res(executor,fun_name,arg_list):
        i = 1/0
        return list(executor.map(fun_name, arg_list))
    
    def task_func(i):
        try:
            if i == 'a':
                i = i/0
                return {'status': 0, 'msg': i}
    
            if i == 'b':
                return {'status': 1, 'msg': i}
    
            if i == 'c':
                return {'status': 0, 'msg': i}
    
        except Exception as e:
            return {'status': -1, 'msg': e}
    
    def f1():
        # 1.thread_arg
        thread_arg=['a','b','c']
        try:
        # 2.提交多线程
            multithread_res_list = get_multithreading_res(settings.common_executor, task_func, thread_arg)
            print('multithread_res_list',multithread_res_list)
    
            # 3.判断结果:
            for i_thread_res in multithread_res_list:
                if i_thread_res['status'] ==1:
                    print('返回成功',i_thread_res)
                elif i_thread_res['status'] ==0:
                    print('返回失败',i_thread_res)
    
                else:
                    print('调用task_func有异常', i_thread_res['msg'])
    
        except Exception as e:
            print('执行f1函数发生异常:%s' %e)
    
    f1()


    2.任务函数不做异常处理,出异常后,多线程中断:

    # -*- coding: utf-8 -*-def get_multithreading_res(executor,fun_name,arg_list):
        return list(executor.map(fun_name, arg_list))
    
    def task_func(i):
    
        if i == 'a':
            i = i/0
            return {'status': 0, 'msg': i}
    
        if i == 'b':
            return {'status': 1, 'msg': i}
    
        if i == 'c':
            return {'status': 0, 'msg': i}
    
    
    def f1():
        # 1.thread_arg
        thread_arg=['a','b','c']
        try:
        # 2.提交多线程
            multithread_res_list = get_multithreading_res(settings.common_executor, task_func, thread_arg)
            print('multithread_res_list',multithread_res_list)
            # 3.判断结果:
            for i_thread_res in multithread_res_list:
                if i_thread_res['status'] ==1:
                    print('返回成功',i_thread_res)
                elif i_thread_res['status'] ==0:
                    print('返回失败',i_thread_res)
                else:
                    print('调用task_func有异常', i_thread_res['msg'])
    
        except Exception as e:
            print('执行f1函数发生异常:%s' %e)
    
    f1()

    3.

    # -*- coding: utf-8 -*-def get_multithreading_res(executor,fun_name,arg_list):
        return list(executor.map(fun_name, arg_list))
    
    def task_func(i):
        try:
            if i == 'a':
                i = i/0
                return {'status': 0, 'msg': i}
    
            if i == 'b':
                return {'status': 1, 'msg': i}
    
            if i == 'c':
                return {'status': 0, 'msg': i}
    
        except Exception as e:
            return {'status': -1, 'msg': e}
    
    def f1():
        # 1.thread_arg
        thread_arg=['a','b','c']
        try:
        # 2.提交多线程
            multithread_res_list = get_multithreading_res(settings.common_executor, task_func, thread_arg)
            print('multithread_res_list',multithread_res_list)
    
            # 3.判断结果:
            for i_thread_res in multithread_res_list:
                if i_thread_res['status'] ==1:
                    print('返回成功',i_thread_res)
                elif i_thread_res['status'] ==0:
                    print('返回失败',i_thread_res)
                else:
                    print('调用task_func有异常', i_thread_res['msg'])
    
        except Exception as e:
            print('执行f1函数发生异常:%s' %e)
    
    f1()

    2.

  • 相关阅读:
    面试40-一个数组,有2个数字出现奇数次,其余都是偶数次,求这两个数字O(n) O(1)
    面试38-数字在排序数组中出现的个数
    面试35-删除字符串重复字符-删除出现在第二个字符串中的字符-第一个只出现一次的字符-hash表计数
    意外get接近完美的黑苹果 (UEFI + GPT)
    Windows 启用/禁用内置管理员 Administrator
    出去走走
    【搬运】Wget 命令详解
    C语言学习之插入排序
    由 UWP 版网易云音乐闪退引发的博文
    gets() 与 scanf() 的小尴尬
  • 原文地址:https://www.cnblogs.com/dingyunfeng/p/11261085.html
Copyright © 2011-2022 走看看