首页 > 解决方案 > fork 一个 Python 子进程以在解释器、SSH 死亡、核战争中幸存下来

问题描述

这是两天的斗争,我很困惑。

TLDR:从 Python shell ( >>> "like this") 中,启动一个 Python 脚本,quit()然后me@terminal:~$ exit.


我努力了:

玩具代码- Python 3.6

class MyClass(threading.Thread):
    def __init__(self, filepath):
        super().__init__()
        self.filepath = filepath

    def run(self):
        import time
        i = 0
        while True:  # Count to 50 in a file forever
            if i > 50: i = 0
            with open(self.filepath, "a") as afile:
                afile.writelines("This is msg {}\n".format(i))
            i += 1
            time.sleep(1)

如何Thread在 Python shell 中运行它,以使其在关闭解释器并关闭 SSH 终端时不会死机?


Real Code Django 管理的 PubNub 消息的 DB 消费

# TODO线,靠近中心。类实例将PNRunner在 Django 应用程序的构造中动态启动和终止,因此我不能只在启动时使用 shell 脚本启动它们。

我真正想做的事

标签: pythondjangosubprocess

解决方案


推荐阅读