python multiprocessing is loosing values











up vote
2
down vote

favorite












I tried to speed up a calculation using Pool from the multiprocessing package.
While I did get a significant speedup I'm missing more and more values as I increase the core/worker count.



I share my variables with all processes through the mp.value() class.



Where did i go wrong and how can i fix this?



poss = [x+1 for x in range(20)]
all_rolls = itertools.product(poss, repeat=6)

win = mp.Value('i', 0)
draw = mp.Value('i', 0)
loose = mp.Value('i', 0)

def some_func(roll):
if(comparison on rolls):
win.value += 1
elif(other comparison):
draw.value +=1
else:
loose.value +=1

with Pool(8) as p:
p.map(some_func, all_rolls)


On 16 cores i got 55,923,638 values instead of 64,000,000










share|improve this question


























    up vote
    2
    down vote

    favorite












    I tried to speed up a calculation using Pool from the multiprocessing package.
    While I did get a significant speedup I'm missing more and more values as I increase the core/worker count.



    I share my variables with all processes through the mp.value() class.



    Where did i go wrong and how can i fix this?



    poss = [x+1 for x in range(20)]
    all_rolls = itertools.product(poss, repeat=6)

    win = mp.Value('i', 0)
    draw = mp.Value('i', 0)
    loose = mp.Value('i', 0)

    def some_func(roll):
    if(comparison on rolls):
    win.value += 1
    elif(other comparison):
    draw.value +=1
    else:
    loose.value +=1

    with Pool(8) as p:
    p.map(some_func, all_rolls)


    On 16 cores i got 55,923,638 values instead of 64,000,000










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I tried to speed up a calculation using Pool from the multiprocessing package.
      While I did get a significant speedup I'm missing more and more values as I increase the core/worker count.



      I share my variables with all processes through the mp.value() class.



      Where did i go wrong and how can i fix this?



      poss = [x+1 for x in range(20)]
      all_rolls = itertools.product(poss, repeat=6)

      win = mp.Value('i', 0)
      draw = mp.Value('i', 0)
      loose = mp.Value('i', 0)

      def some_func(roll):
      if(comparison on rolls):
      win.value += 1
      elif(other comparison):
      draw.value +=1
      else:
      loose.value +=1

      with Pool(8) as p:
      p.map(some_func, all_rolls)


      On 16 cores i got 55,923,638 values instead of 64,000,000










      share|improve this question













      I tried to speed up a calculation using Pool from the multiprocessing package.
      While I did get a significant speedup I'm missing more and more values as I increase the core/worker count.



      I share my variables with all processes through the mp.value() class.



      Where did i go wrong and how can i fix this?



      poss = [x+1 for x in range(20)]
      all_rolls = itertools.product(poss, repeat=6)

      win = mp.Value('i', 0)
      draw = mp.Value('i', 0)
      loose = mp.Value('i', 0)

      def some_func(roll):
      if(comparison on rolls):
      win.value += 1
      elif(other comparison):
      draw.value +=1
      else:
      loose.value +=1

      with Pool(8) as p:
      p.map(some_func, all_rolls)


      On 16 cores i got 55,923,638 values instead of 64,000,000







      python multiprocessing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Eumel

      15911




      15911
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You need to protect the modification of your values with Lock (see this article).



          from multiprocessing import Lock

          lock = Lock()

          def some_func(roll):
          with lock:
          if(comparison on rolls):
          win.value += 1
          elif(other comparison):
          draw.value +=1
          else:
          loose.value +=1





          share|improve this answer




























            up vote
            2
            down vote













            In addition to what @jfowkes answered, note that you can use each Value with its own lock, which might make things a bit faster:



            win = mp.Value('i', lock = True)
            draw = mp.Value('i', lock = True)
            loose = mp.Value('i', lock = True)

            def some_func(roll):
            if(comparison on rolls):
            with win.get_lock() :
            win.value += 1
            elif(other comparison):
            with draw.get_lock():
            draw.value +=1
            else:
            with loose.get_lock():
            loose.value +=1





            share|improve this answer

















            • 1




              Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
              – jfowkes
              yesterday











            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',
            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%2f53409126%2fpython-multiprocessing-is-loosing-values%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            You need to protect the modification of your values with Lock (see this article).



            from multiprocessing import Lock

            lock = Lock()

            def some_func(roll):
            with lock:
            if(comparison on rolls):
            win.value += 1
            elif(other comparison):
            draw.value +=1
            else:
            loose.value +=1





            share|improve this answer

























              up vote
              3
              down vote



              accepted










              You need to protect the modification of your values with Lock (see this article).



              from multiprocessing import Lock

              lock = Lock()

              def some_func(roll):
              with lock:
              if(comparison on rolls):
              win.value += 1
              elif(other comparison):
              draw.value +=1
              else:
              loose.value +=1





              share|improve this answer























                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                You need to protect the modification of your values with Lock (see this article).



                from multiprocessing import Lock

                lock = Lock()

                def some_func(roll):
                with lock:
                if(comparison on rolls):
                win.value += 1
                elif(other comparison):
                draw.value +=1
                else:
                loose.value +=1





                share|improve this answer












                You need to protect the modification of your values with Lock (see this article).



                from multiprocessing import Lock

                lock = Lock()

                def some_func(roll):
                with lock:
                if(comparison on rolls):
                win.value += 1
                elif(other comparison):
                draw.value +=1
                else:
                loose.value +=1






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered yesterday









                jfowkes

                662311




                662311
























                    up vote
                    2
                    down vote













                    In addition to what @jfowkes answered, note that you can use each Value with its own lock, which might make things a bit faster:



                    win = mp.Value('i', lock = True)
                    draw = mp.Value('i', lock = True)
                    loose = mp.Value('i', lock = True)

                    def some_func(roll):
                    if(comparison on rolls):
                    with win.get_lock() :
                    win.value += 1
                    elif(other comparison):
                    with draw.get_lock():
                    draw.value +=1
                    else:
                    with loose.get_lock():
                    loose.value +=1





                    share|improve this answer

















                    • 1




                      Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
                      – jfowkes
                      yesterday















                    up vote
                    2
                    down vote













                    In addition to what @jfowkes answered, note that you can use each Value with its own lock, which might make things a bit faster:



                    win = mp.Value('i', lock = True)
                    draw = mp.Value('i', lock = True)
                    loose = mp.Value('i', lock = True)

                    def some_func(roll):
                    if(comparison on rolls):
                    with win.get_lock() :
                    win.value += 1
                    elif(other comparison):
                    with draw.get_lock():
                    draw.value +=1
                    else:
                    with loose.get_lock():
                    loose.value +=1





                    share|improve this answer

















                    • 1




                      Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
                      – jfowkes
                      yesterday













                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    In addition to what @jfowkes answered, note that you can use each Value with its own lock, which might make things a bit faster:



                    win = mp.Value('i', lock = True)
                    draw = mp.Value('i', lock = True)
                    loose = mp.Value('i', lock = True)

                    def some_func(roll):
                    if(comparison on rolls):
                    with win.get_lock() :
                    win.value += 1
                    elif(other comparison):
                    with draw.get_lock():
                    draw.value +=1
                    else:
                    with loose.get_lock():
                    loose.value +=1





                    share|improve this answer












                    In addition to what @jfowkes answered, note that you can use each Value with its own lock, which might make things a bit faster:



                    win = mp.Value('i', lock = True)
                    draw = mp.Value('i', lock = True)
                    loose = mp.Value('i', lock = True)

                    def some_func(roll):
                    if(comparison on rolls):
                    with win.get_lock() :
                    win.value += 1
                    elif(other comparison):
                    with draw.get_lock():
                    draw.value +=1
                    else:
                    with loose.get_lock():
                    loose.value +=1






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered yesterday









                    Or Dinari

                    700318




                    700318








                    • 1




                      Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
                      – jfowkes
                      yesterday














                    • 1




                      Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
                      – jfowkes
                      yesterday








                    1




                    1




                    Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
                    – jfowkes
                    yesterday




                    Thanks, I wasn't sure about the relative speed of things so I kept my answer simple. This is useful to know.
                    – jfowkes
                    yesterday


















                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409126%2fpython-multiprocessing-is-loosing-values%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

                    Lallio

                    Futebolista

                    Jornalista