Python Multiprocessing - Object has no attribute












1















I'm trying to use multiprocessing on my script. But it doesn't work. what am I doing wrong? I searched too much but I didn't find the solution. Can you help me guys?



It seems HistogramMerger working with multiprocessing. I saw some print-out when I run the script but I don't get any result file which I normally getting with for loop.



I'm getting this error message:



AttributeError: 'module' object has no attribute 'histogramAdd'


ps: This histogram merger script merging multiple files to one single file. And, I'm trying to run this script faster than normal. If you know better solution, please let me know.



without multiprocessing (working)



from histogram_merger import HistogramMerger

var1=697
var2=722
with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var1)+".root") as hm:
for i in xrange(var1,var2+1):
print "Run Number : " +str(i)
hm.addHistogramFile("../results/run"+str(i)+"/run"+str(i)+"_histo.root")


with MultiProcessing



from histogram_merger import HistogramMerger
from multiprocessing import Pool

var1=697
var2=722

##################################################

arrayOfNumbers = [xx for xx in range(var1, var2+1)]
print(arrayOfNumbers)
pool = Pool(8)

def histogramAdd(run):
print("Run Number : "+str(run))
hm.addHistogramFile("../results/run"+str(run)+"/run"+str(run)+"_histo.root")

if __name__ == '__main__':
with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var2)+".root") as hm:
pool.map(histogramAdd, arrayOfNumbers)
pool.join()









share|improve this question



























    1















    I'm trying to use multiprocessing on my script. But it doesn't work. what am I doing wrong? I searched too much but I didn't find the solution. Can you help me guys?



    It seems HistogramMerger working with multiprocessing. I saw some print-out when I run the script but I don't get any result file which I normally getting with for loop.



    I'm getting this error message:



    AttributeError: 'module' object has no attribute 'histogramAdd'


    ps: This histogram merger script merging multiple files to one single file. And, I'm trying to run this script faster than normal. If you know better solution, please let me know.



    without multiprocessing (working)



    from histogram_merger import HistogramMerger

    var1=697
    var2=722
    with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var1)+".root") as hm:
    for i in xrange(var1,var2+1):
    print "Run Number : " +str(i)
    hm.addHistogramFile("../results/run"+str(i)+"/run"+str(i)+"_histo.root")


    with MultiProcessing



    from histogram_merger import HistogramMerger
    from multiprocessing import Pool

    var1=697
    var2=722

    ##################################################

    arrayOfNumbers = [xx for xx in range(var1, var2+1)]
    print(arrayOfNumbers)
    pool = Pool(8)

    def histogramAdd(run):
    print("Run Number : "+str(run))
    hm.addHistogramFile("../results/run"+str(run)+"/run"+str(run)+"_histo.root")

    if __name__ == '__main__':
    with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var2)+".root") as hm:
    pool.map(histogramAdd, arrayOfNumbers)
    pool.join()









    share|improve this question

























      1












      1








      1








      I'm trying to use multiprocessing on my script. But it doesn't work. what am I doing wrong? I searched too much but I didn't find the solution. Can you help me guys?



      It seems HistogramMerger working with multiprocessing. I saw some print-out when I run the script but I don't get any result file which I normally getting with for loop.



      I'm getting this error message:



      AttributeError: 'module' object has no attribute 'histogramAdd'


      ps: This histogram merger script merging multiple files to one single file. And, I'm trying to run this script faster than normal. If you know better solution, please let me know.



      without multiprocessing (working)



      from histogram_merger import HistogramMerger

      var1=697
      var2=722
      with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var1)+".root") as hm:
      for i in xrange(var1,var2+1):
      print "Run Number : " +str(i)
      hm.addHistogramFile("../results/run"+str(i)+"/run"+str(i)+"_histo.root")


      with MultiProcessing



      from histogram_merger import HistogramMerger
      from multiprocessing import Pool

      var1=697
      var2=722

      ##################################################

      arrayOfNumbers = [xx for xx in range(var1, var2+1)]
      print(arrayOfNumbers)
      pool = Pool(8)

      def histogramAdd(run):
      print("Run Number : "+str(run))
      hm.addHistogramFile("../results/run"+str(run)+"/run"+str(run)+"_histo.root")

      if __name__ == '__main__':
      with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var2)+".root") as hm:
      pool.map(histogramAdd, arrayOfNumbers)
      pool.join()









      share|improve this question














      I'm trying to use multiprocessing on my script. But it doesn't work. what am I doing wrong? I searched too much but I didn't find the solution. Can you help me guys?



      It seems HistogramMerger working with multiprocessing. I saw some print-out when I run the script but I don't get any result file which I normally getting with for loop.



      I'm getting this error message:



      AttributeError: 'module' object has no attribute 'histogramAdd'


      ps: This histogram merger script merging multiple files to one single file. And, I'm trying to run this script faster than normal. If you know better solution, please let me know.



      without multiprocessing (working)



      from histogram_merger import HistogramMerger

      var1=697
      var2=722
      with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var1)+".root") as hm:
      for i in xrange(var1,var2+1):
      print "Run Number : " +str(i)
      hm.addHistogramFile("../results/run"+str(i)+"/run"+str(i)+"_histo.root")


      with MultiProcessing



      from histogram_merger import HistogramMerger
      from multiprocessing import Pool

      var1=697
      var2=722

      ##################################################

      arrayOfNumbers = [xx for xx in range(var1, var2+1)]
      print(arrayOfNumbers)
      pool = Pool(8)

      def histogramAdd(run):
      print("Run Number : "+str(run))
      hm.addHistogramFile("../results/run"+str(run)+"/run"+str(run)+"_histo.root")

      if __name__ == '__main__':
      with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var2)+".root") as hm:
      pool.map(histogramAdd, arrayOfNumbers)
      pool.join()






      python multiprocessing python-multiprocessing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 16:26









      AliAli

      61




      61
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The error message is odd. hm is not in scope within the function histogramAdd. I would expect something like NameError: name 'hm' is not defined. Perhaps there is some hm import you are not showing.



          Regardless, you need to pass the object to the function. You can use functools.partial for this. E.g.



          from functools import partial

          # ...

          def histogramAdd(run, hm): # <- extra parameter!
          print("Run Number : "+str(run))
          hm.addHistogramFile("../results/run"+str(run)+"/run"+str(run)+"_histo.root")

          if __name__ == '__main__':
          with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var2)+".root") as hm:
          pool.map(partial(histogramAdd, hm), arrayOfNumbers)
          pool.join()





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450109%2fpython-multiprocessing-object-has-no-attribute%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The error message is odd. hm is not in scope within the function histogramAdd. I would expect something like NameError: name 'hm' is not defined. Perhaps there is some hm import you are not showing.



            Regardless, you need to pass the object to the function. You can use functools.partial for this. E.g.



            from functools import partial

            # ...

            def histogramAdd(run, hm): # <- extra parameter!
            print("Run Number : "+str(run))
            hm.addHistogramFile("../results/run"+str(run)+"/run"+str(run)+"_histo.root")

            if __name__ == '__main__':
            with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var2)+".root") as hm:
            pool.map(partial(histogramAdd, hm), arrayOfNumbers)
            pool.join()





            share|improve this answer




























              0














              The error message is odd. hm is not in scope within the function histogramAdd. I would expect something like NameError: name 'hm' is not defined. Perhaps there is some hm import you are not showing.



              Regardless, you need to pass the object to the function. You can use functools.partial for this. E.g.



              from functools import partial

              # ...

              def histogramAdd(run, hm): # <- extra parameter!
              print("Run Number : "+str(run))
              hm.addHistogramFile("../results/run"+str(run)+"/run"+str(run)+"_histo.root")

              if __name__ == '__main__':
              with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var2)+".root") as hm:
              pool.map(partial(histogramAdd, hm), arrayOfNumbers)
              pool.join()





              share|improve this answer


























                0












                0








                0







                The error message is odd. hm is not in scope within the function histogramAdd. I would expect something like NameError: name 'hm' is not defined. Perhaps there is some hm import you are not showing.



                Regardless, you need to pass the object to the function. You can use functools.partial for this. E.g.



                from functools import partial

                # ...

                def histogramAdd(run, hm): # <- extra parameter!
                print("Run Number : "+str(run))
                hm.addHistogramFile("../results/run"+str(run)+"/run"+str(run)+"_histo.root")

                if __name__ == '__main__':
                with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var2)+".root") as hm:
                pool.map(partial(histogramAdd, hm), arrayOfNumbers)
                pool.join()





                share|improve this answer













                The error message is odd. hm is not in scope within the function histogramAdd. I would expect something like NameError: name 'hm' is not defined. Perhaps there is some hm import you are not showing.



                Regardless, you need to pass the object to the function. You can use functools.partial for this. E.g.



                from functools import partial

                # ...

                def histogramAdd(run, hm): # <- extra parameter!
                print("Run Number : "+str(run))
                hm.addHistogramFile("../results/run"+str(run)+"/run"+str(run)+"_histo.root")

                if __name__ == '__main__':
                with HistogramMerger("results/resMergedHistograms_"+str(var1)+"_"+str(var2)+".root") as hm:
                pool.map(partial(histogramAdd, hm), arrayOfNumbers)
                pool.join()






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 16:40









                JETMJETM

                1,93741627




                1,93741627
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450109%2fpython-multiprocessing-object-has-no-attribute%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen