多发

2024-04-28 06:03:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用多重处理。我的CPU有四个内核,我将池的进程设置为2

1)如果我同时运行同一个程序(多次运行相同的时间),它将使用我的所有内核?是的,如果一个内核同时运行,如何设置它总是空闲的

2)我为每个国家/地区单独运行没有问题,但如果我与不同国家/地区同时运行上述文件,它将在7-8小时后挂起

你知道为什么会被绞死吗?你知道怎么追踪绞死的原因吗

import  time
from multiprocessing import Pool
from functools import partial
import os

class Test:
    country = ''

    def __init__(self, countryCode):
        self.country = countryCode

    def Mainfn_New(self):
        #around 10-20 files
        inputpath = "C:\\" + self.country + "\\Inputfiles"
        dirlist = os.listdir(inputpath)
        print(inputpath)
        print(dirlist)
        print("START Main")

        pool = Pool(2)
        func = partial(self.HugeProcess, inputpath)
        results = pool.map(func, dirlist)
        pool.close()
        pool.join()
        print("End Main")

    def HugeProcess(self, inputpath, filename):
        #Proces will take around 10-30 mins to complete the process
        print("START HugeProcess ***" , filename)
        #time.sleep(1)
        print("END HugeProcess ***", filename)


if __name__ == '__main__':
    print("Starting .........")
    #country code MY
    obj = Test('MY')
    obj.Mainfn_New()

Tags: fromimportselftimedef国家filename内核