Instance variable of multiprocessing Process in Python3 not being written on terminate











up vote
1
down vote

favorite












A surprising thing which I came across while writing a logic of saving some value during process termination was a bit strange for me. Writing a toy example program to show the problem.



import multiprocessing
import time

class A(multiprocessing.Process):
def __init__(self):
self.till = 0
super(A, self).__init__()

def run(self):
i = 0
while True:
print(i)
i += 1
self.till = i
time.sleep(1)

def terminate(self):
print("Terminating : {}".format(self.till))
super(A, self).terminate()

if __name__ == "__main__":
obj = A()
obj.start()
time.sleep(5)
obj.terminate()


The output for the above program is -



0
1
2
3
4
Terminating : 0


Why is terminate() not printing out 4? Anything I am missing?










share|improve this question


























    up vote
    1
    down vote

    favorite












    A surprising thing which I came across while writing a logic of saving some value during process termination was a bit strange for me. Writing a toy example program to show the problem.



    import multiprocessing
    import time

    class A(multiprocessing.Process):
    def __init__(self):
    self.till = 0
    super(A, self).__init__()

    def run(self):
    i = 0
    while True:
    print(i)
    i += 1
    self.till = i
    time.sleep(1)

    def terminate(self):
    print("Terminating : {}".format(self.till))
    super(A, self).terminate()

    if __name__ == "__main__":
    obj = A()
    obj.start()
    time.sleep(5)
    obj.terminate()


    The output for the above program is -



    0
    1
    2
    3
    4
    Terminating : 0


    Why is terminate() not printing out 4? Anything I am missing?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      A surprising thing which I came across while writing a logic of saving some value during process termination was a bit strange for me. Writing a toy example program to show the problem.



      import multiprocessing
      import time

      class A(multiprocessing.Process):
      def __init__(self):
      self.till = 0
      super(A, self).__init__()

      def run(self):
      i = 0
      while True:
      print(i)
      i += 1
      self.till = i
      time.sleep(1)

      def terminate(self):
      print("Terminating : {}".format(self.till))
      super(A, self).terminate()

      if __name__ == "__main__":
      obj = A()
      obj.start()
      time.sleep(5)
      obj.terminate()


      The output for the above program is -



      0
      1
      2
      3
      4
      Terminating : 0


      Why is terminate() not printing out 4? Anything I am missing?










      share|improve this question













      A surprising thing which I came across while writing a logic of saving some value during process termination was a bit strange for me. Writing a toy example program to show the problem.



      import multiprocessing
      import time

      class A(multiprocessing.Process):
      def __init__(self):
      self.till = 0
      super(A, self).__init__()

      def run(self):
      i = 0
      while True:
      print(i)
      i += 1
      self.till = i
      time.sleep(1)

      def terminate(self):
      print("Terminating : {}".format(self.till))
      super(A, self).terminate()

      if __name__ == "__main__":
      obj = A()
      obj.start()
      time.sleep(5)
      obj.terminate()


      The output for the above program is -



      0
      1
      2
      3
      4
      Terminating : 0


      Why is terminate() not printing out 4? Anything I am missing?







      python multiprocessing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 14:54









      Saurav Das

      153




      153
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          What you are doing is actually running terminate on the main process, look at this code:



          class A(multiprocessing.Process):
          def __init__(self):
          self.till = 0
          super(A, self).__init__()

          def run(self):
          i = 0
          print(os.getpid())
          while True:
          print(i)
          i += 1
          self.till = i
          time.sleep(1)

          def terminate(self):
          print("Terminating : {}".format(self.till))
          print(os.getpid())
          super(A, self).terminate()

          if __name__ == "__main__":
          print("parent pid:")
          print(os.getpid())
          print("child pid:")
          obj = A()
          obj.start()
          time.sleep(3)
          obj.terminate()


          Will lead to the output:



          parent pid:
          12111
          child pid:
          12222
          0
          1
          2
          Terminating : 0
          12111


          At terminate, you are actually sending SIGTERM to the child process, it is done from the parent process, thus the memory is of the parent, where there was no increments to self.till






          share|improve this answer





















          • Thanks for the explanation..
            – Saurav Das
            Nov 10 at 15:46


















          up vote
          0
          down vote













          init and terminate methods run on the main process hence the sub-process prints 0 for your terminate function. Run method only increments in the sub process. You can confirm this by using os.getpid() method in python.



          Edit: This problem probably only occurs in Windows since it does not have a fork() system call like in Linux/Unix systems. Windows starts the whole module from the beginning to achieve the effect.






          share|improve this answer





















          • Thanks.. I'm using Linux and the problem occurs there.
            – Saurav Das
            Nov 10 at 15:45











          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%2f53240139%2finstance-variable-of-multiprocessing-process-in-python3-not-being-written-on-ter%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
          0
          down vote



          accepted










          What you are doing is actually running terminate on the main process, look at this code:



          class A(multiprocessing.Process):
          def __init__(self):
          self.till = 0
          super(A, self).__init__()

          def run(self):
          i = 0
          print(os.getpid())
          while True:
          print(i)
          i += 1
          self.till = i
          time.sleep(1)

          def terminate(self):
          print("Terminating : {}".format(self.till))
          print(os.getpid())
          super(A, self).terminate()

          if __name__ == "__main__":
          print("parent pid:")
          print(os.getpid())
          print("child pid:")
          obj = A()
          obj.start()
          time.sleep(3)
          obj.terminate()


          Will lead to the output:



          parent pid:
          12111
          child pid:
          12222
          0
          1
          2
          Terminating : 0
          12111


          At terminate, you are actually sending SIGTERM to the child process, it is done from the parent process, thus the memory is of the parent, where there was no increments to self.till






          share|improve this answer





















          • Thanks for the explanation..
            – Saurav Das
            Nov 10 at 15:46















          up vote
          0
          down vote



          accepted










          What you are doing is actually running terminate on the main process, look at this code:



          class A(multiprocessing.Process):
          def __init__(self):
          self.till = 0
          super(A, self).__init__()

          def run(self):
          i = 0
          print(os.getpid())
          while True:
          print(i)
          i += 1
          self.till = i
          time.sleep(1)

          def terminate(self):
          print("Terminating : {}".format(self.till))
          print(os.getpid())
          super(A, self).terminate()

          if __name__ == "__main__":
          print("parent pid:")
          print(os.getpid())
          print("child pid:")
          obj = A()
          obj.start()
          time.sleep(3)
          obj.terminate()


          Will lead to the output:



          parent pid:
          12111
          child pid:
          12222
          0
          1
          2
          Terminating : 0
          12111


          At terminate, you are actually sending SIGTERM to the child process, it is done from the parent process, thus the memory is of the parent, where there was no increments to self.till






          share|improve this answer





















          • Thanks for the explanation..
            – Saurav Das
            Nov 10 at 15:46













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          What you are doing is actually running terminate on the main process, look at this code:



          class A(multiprocessing.Process):
          def __init__(self):
          self.till = 0
          super(A, self).__init__()

          def run(self):
          i = 0
          print(os.getpid())
          while True:
          print(i)
          i += 1
          self.till = i
          time.sleep(1)

          def terminate(self):
          print("Terminating : {}".format(self.till))
          print(os.getpid())
          super(A, self).terminate()

          if __name__ == "__main__":
          print("parent pid:")
          print(os.getpid())
          print("child pid:")
          obj = A()
          obj.start()
          time.sleep(3)
          obj.terminate()


          Will lead to the output:



          parent pid:
          12111
          child pid:
          12222
          0
          1
          2
          Terminating : 0
          12111


          At terminate, you are actually sending SIGTERM to the child process, it is done from the parent process, thus the memory is of the parent, where there was no increments to self.till






          share|improve this answer












          What you are doing is actually running terminate on the main process, look at this code:



          class A(multiprocessing.Process):
          def __init__(self):
          self.till = 0
          super(A, self).__init__()

          def run(self):
          i = 0
          print(os.getpid())
          while True:
          print(i)
          i += 1
          self.till = i
          time.sleep(1)

          def terminate(self):
          print("Terminating : {}".format(self.till))
          print(os.getpid())
          super(A, self).terminate()

          if __name__ == "__main__":
          print("parent pid:")
          print(os.getpid())
          print("child pid:")
          obj = A()
          obj.start()
          time.sleep(3)
          obj.terminate()


          Will lead to the output:



          parent pid:
          12111
          child pid:
          12222
          0
          1
          2
          Terminating : 0
          12111


          At terminate, you are actually sending SIGTERM to the child process, it is done from the parent process, thus the memory is of the parent, where there was no increments to self.till







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 15:21









          Or Dinari

          450216




          450216












          • Thanks for the explanation..
            – Saurav Das
            Nov 10 at 15:46


















          • Thanks for the explanation..
            – Saurav Das
            Nov 10 at 15:46
















          Thanks for the explanation..
          – Saurav Das
          Nov 10 at 15:46




          Thanks for the explanation..
          – Saurav Das
          Nov 10 at 15:46












          up vote
          0
          down vote













          init and terminate methods run on the main process hence the sub-process prints 0 for your terminate function. Run method only increments in the sub process. You can confirm this by using os.getpid() method in python.



          Edit: This problem probably only occurs in Windows since it does not have a fork() system call like in Linux/Unix systems. Windows starts the whole module from the beginning to achieve the effect.






          share|improve this answer





















          • Thanks.. I'm using Linux and the problem occurs there.
            – Saurav Das
            Nov 10 at 15:45















          up vote
          0
          down vote













          init and terminate methods run on the main process hence the sub-process prints 0 for your terminate function. Run method only increments in the sub process. You can confirm this by using os.getpid() method in python.



          Edit: This problem probably only occurs in Windows since it does not have a fork() system call like in Linux/Unix systems. Windows starts the whole module from the beginning to achieve the effect.






          share|improve this answer





















          • Thanks.. I'm using Linux and the problem occurs there.
            – Saurav Das
            Nov 10 at 15:45













          up vote
          0
          down vote










          up vote
          0
          down vote









          init and terminate methods run on the main process hence the sub-process prints 0 for your terminate function. Run method only increments in the sub process. You can confirm this by using os.getpid() method in python.



          Edit: This problem probably only occurs in Windows since it does not have a fork() system call like in Linux/Unix systems. Windows starts the whole module from the beginning to achieve the effect.






          share|improve this answer












          init and terminate methods run on the main process hence the sub-process prints 0 for your terminate function. Run method only increments in the sub process. You can confirm this by using os.getpid() method in python.



          Edit: This problem probably only occurs in Windows since it does not have a fork() system call like in Linux/Unix systems. Windows starts the whole module from the beginning to achieve the effect.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 15:12









          Berkays

          12315




          12315












          • Thanks.. I'm using Linux and the problem occurs there.
            – Saurav Das
            Nov 10 at 15:45


















          • Thanks.. I'm using Linux and the problem occurs there.
            – Saurav Das
            Nov 10 at 15:45
















          Thanks.. I'm using Linux and the problem occurs there.
          – Saurav Das
          Nov 10 at 15:45




          Thanks.. I'm using Linux and the problem occurs there.
          – Saurav Das
          Nov 10 at 15:45


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240139%2finstance-variable-of-multiprocessing-process-in-python3-not-being-written-on-ter%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

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma