首页 > 解决方案 > 多处理 some_process.start() 不执行函数

问题描述

在下面的代码片段中, .start() 不执行该方法。self.listen_items() 中的第一个打印语句永远不会打印。有一个实例化一个类的主文件,用于根据需要安排和管理其他类的实例化。这里的代码来自这些类之一。我整个上午都在寻找有关问题的提示。没有发现任何与我的情况有关的情况。有任何想法吗?

self.get_items_process = Process(target=self.listen_items, daemon=True, args=()) 
self.get_items_process.start()

def listen_items(self):
    """Method replaces listen_to_scheduler_utility() so we can manage exiting the thread on a test restart."""
    print("@@@ listen_items(start) @@@")
    while not self._restart_pending:
        print("@@@ listen_items(loop) @@@")
        item: QueueItem = self._scheduler_utility.get_item_from_queues(STR_ITERATION_CONTROLLER, self._iteration)
        if item is not None:
            print("@@@ listen_items(item) @@@")
            self.process_input(queue_item=item)

"""

标签: pythonmultiprocessing

解决方案


所以,这就是我发现的。首先,start() 方法一直在工作。一个 shell 脚本启动应用程序进程,我通常将 stdout 和 stderr 重定向到一个文件。打印语句未显示在该文件中;但是,如果我没有重定向输出,看哪,它们会被喷到终端屏幕上。我对为什么他们没有出现在重定向中有点模糊,但我现在明白发生了什么。Linux 机器上的多处理执行分叉主线程,因此分叉进程正在写入消息,但父进程是唯一写入日志文件的进程。感谢所有的评论...


推荐阅读