How to retrieve multiple values returned of a function called through multiprocessing.Process

后端 未结 1 706
闹比i
闹比i 2021-02-13 05:03

I have a scenario like this :

for each in content :
     pdf_output,job_id=createpdf(each)
     if pdf_output : 
        pdf_output = pdf_output + pdf_output
         


        
1条回答
  •  再見小時候
    2021-02-13 05:20

    You do not need queues for such a simple task. I would recommend to use pools. The Pool.map method can apply a function to a series of values in parallel:

    import multiprocessing
    def createpdf(data):
        return ("This is my pdf data: %s\n" % data, 0)
    
    
    data = [ "My data", "includes", "strings and", "numbers like", 42, "and", 3.14]
    number_of_processes = 5
    results = multiprocessing.Pool(number_of_processes).map(createpdf, data)
    outputs = [result[0] for result in results]
    pdfoutput = "".join(outputs)
    

    0 讨论(0)
提交回复
热议问题