简体   繁体   English

Python3 进程未在 sys.exit() 上关闭

[英]Python3 process not closing on sys.exit()

So i am working on a project, now in python2 the following code works fine.所以我正在做一个项目,现在在 python2 中,下面的代码工作正常。 But i want to upgrade to python3 as python2 is no longer supported.但我想升级到 python3,因为不再支持 python2。 So in the pin_event_down function a process, backup is started.所以在pin_event_down 函数的一个进程中,备份就开始了。 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?在 python2 中 sys.exit() 工作正常,在阅读 python 文档时,他们说你应该使用 system.exit() 有没有人知道为什么这个过程永远不会结束? 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.此外,每次特定引脚断开时,它会再次使用相同的 RAM 内存再次创建进程,从而用 1 个进程的双倍填充 ram。

    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()

正在运行的进程图片

I think it's because you're calling sys.exit() from a Process.我认为这是因为您从进程中调用 sys.exit() 。 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.一个非常混乱的方法是在进程中使用os._exit(1) ,但这是危险的,所以我不推荐它。

This thread may be of some help to you: How to exit the entire application from a Python thread?此线程可能对您有所帮助: 如何从 Python 线程退出整个应用程序?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM