python - 多处理 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)
"""
解决方案
所以,这就是我发现的。首先,start() 方法一直在工作。一个 shell 脚本启动应用程序进程,我通常将 stdout 和 stderr 重定向到一个文件。打印语句未显示在该文件中;但是,如果我没有重定向输出,看哪,它们会被喷到终端屏幕上。我对为什么他们没有出现在重定向中有点模糊,但我现在明白发生了什么。Linux 机器上的多处理执行分叉主线程,因此分叉进程正在写入消息,但父进程是唯一写入日志文件的进程。感谢所有的评论...
推荐阅读
- javascript - 使用 Google Drive API 创建文件时,文件内容为空
- python - 在 Python 中使用一个类基于 __init__() 返回不同的对象?
- tensorflow - Object detection Classfication /A checkpoint 已恢复(例如 tf.train.Checkpoint.restore 或 tf.keras.Model.load_weights)
- ios - 如果需要,将底部约束设置为 UITextView
- reactjs - 我想将 reactstrap 和 bootstrap 添加到 React Web Coding
- unity3d - Agora RTC 是否支持 UWP(特别是 Hololens 2)?
- javascript - 使用 fabric.js 在下部画布上添加多个图像
- python - 两个张量的乘积
- javascript - 为什么 Safari 不会停止视频标签上的传播点击事件?
- perl - 如何在 perl 中使用 File::stat 获取文件的本地时间修改?