0

So i am working on a project, now in python2 the following code works fine. But i want to upgrade to python3 as python2 is no longer supported. So in the pin_event_down function a process, backup is started. The backup process and function are starting some data logging thread, and then it should be closed/ killed. In python2 the sys.exit() works fine and when reading the python documentation they say that you should use the system.exit() does anyone have a clue why the process is never ended? Also every time the specific pin goes down it creates the process again using the same RAM memory again, thus filling the ram with doubles of 1 process.

    def pin_event_down(self):
    """
    This function is used to create a product pin event.
    @return: None.
    """
        #do some stuff
        Process(target=self.backUp).start()
        #do some stuff

    def backUp(self):
    """
    This function is used to create a backup feedback log.
    @return: None.
    """
        if product is not None:
            self.logger.__init__()
            self.logger.start()
            #do some stuff
        sys.exit()

Picture of the processes running

Jasper
  • 13
  • 7
  • Do the forked processes _also_ receive the `pin_event_down` call or did you take steps to prevent that? – Thomas Oct 19 '20 at 12:52
  • @Thomas No they should be stand alone. The eventmanager is started in the beginning and i do not belief he has knowledge about the process backup. The eventmanger only handles google api events. – Jasper Oct 19 '20 at 12:54

1 Answers1

0

I think it's because you're calling sys.exit() from a Process. That only terminates the current process, not the parent one. A very messy approach would be to use os._exit(1) from within the Process, but this is dangerous so I do not recommend it.

This thread may be of some help to you: How to exit the entire application from a Python thread?

  • So I just tried os._exit(1) and this seems to work. But since everyone is saying it is not good to do it this way, what other way would work? Plus I only want to kill that 1 single process :process backup. Which seems to be working with os._exit(1) as only that process stops running – Jasper Oct 19 '20 at 13:06
  • If I understand correctly, you only want the one process to be killed. Since it is a Process, can you just have it return? You can also assign the process to a variable, p, and call p.join() which allows the process to terminate. Fairly sure called join() is a blocking call, which might not be what you want – Sam Abarbanel Oct 19 '20 at 13:45