多线程多进程运行python任务

本文介绍了Python中实现多进程和多线程的两种并行处理方式。通过`multiprocessing`库创建多进程,利用`Pool`管理进程池,实现任务分发和错误处理。而在多线程部分,自定义了`PathThreading`类继承自`threading.Thread`,进行线程的创建和结果整合。这两个方法都能提高程序的执行效率,但适用场景不同,多进程适合资源密集型任务,多线程则适合IO密集型任务。
摘要由CSDN通过智能技术生成

文章目录

1.多进程

from multiprocessing import Manager, Pool
import time
import os
import subprocess
from subprocess import PIPE

def throw_exception(name):
    print('子进程%s发生异常,进程号为%s'%(name,os.getpid()))
    cmd = 'taskkill /im '+ str(os.getpid())+' /F'
    res = subprocess.Popen(cmd,shell=True,stdin=PIPE,stdout=PIPE)
    print(res.stdout.read())
    print(res.stderr.read())
    time.sleep(2)

def one_core(my_func, query_id, special_input, share_input, result=None):
    if result is None:
        result = {}
    func_res = my_func(special_input,share_input)
    result[query_id] = func_res
    return result

def run_multi_processing(my_func, input_dict, share_input, threading_num=1):
    result = Manager().dict()
    pool = Pool(processes=threading_num if threading_num >= 8 else threading_num)
    for query_id in input_dict:
        pool.apply_async(one_core, args = (my_func, query_id, input_dict[query_id], share_input, result),error_callback=throw_exception)
    pool.close()
    pool.join()
    return result

2.多线程

import threading

class PathThreading(threading.Thread):
    def __init__(self, threadID, name, my_func, special_input, share_input):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.result_dict = None
        self.my_func = my_func
        self.special_input = special_input
        self.share_input = share_input

    def run(self):
        print("开始线程:" + self.name)
        self.result_dict = self.my_func(self.special_input, self.share_input)
        print("退出线程:" + self.name)


def run_multi_thread(my_func, input_dict, share_input, threading_num=1):
    if threading_num == 1:
        my_func(input_dict, share_input)
    else:
        thread_list = []
        for index in range(threading_num):
            special_input_dict = {k: input_dict[k] for i, k in enumerate(input_dict) if i % threading_num == index}
            thread_list.append(PathThreading(index, "Thread " + str(index), my_func, special_input_dict, share_input))

        for thread in thread_list:
            thread.start()
        for thread in thread_list:
            thread.join()

        overall_result_dict = {}
        for t in thread_list:
            overall_result_dict.update(t.result_dict)
        return overall_result_dict
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值