subprocess.PIPE 在 django 模型中不起作用

问题描述 投票:0回答:0

所以我有这个模型,其中有

isRunning
属性。当它设置为
True
时,它会生成一个子处理实例并将其保存到
subprocessObject
。看起来像这样。

class Monitor(models.Model):
    Name = models.CharField(max_length=143)
    # Some other stuff

    CommandToExecute = models.CharField(max_length=1000, default="python -c print('HelloWorld')")
    subprocessObject = None

    @property
    def isRunning(self):
        return False

    @isRunning.getter
    def isRunning(self):
        if self.subprocessObject is None or self.subprocessObject.poll() is None:
            return False
        return True

    @isRunning.setter
    def isRunning(self, value):
        if value is True and self.isRunning is False:
            self.subprocessObject = subprocess.Popen(self.CommandToExecute.split(" "), stdout=sys.stdout, stdin=sys.stdin, shell=True)
        elif value is False and self.isRunning is True:
            self.subprocessObject.stdin.close()
            self.subprocessObject.stdout.close()
            self.subprocessObject.terminate()
            self.subprocessObject == None

    def __str__(self):
        return self.Name

现在它启动一个文件,每秒打印一些东西

flush=True
.

问题是有一个看起来像这样的错误:

OSError: [Errno 22] Invalid argument
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='cp1252'>
OSError: [Errno 22] Invalid argument

当我将

subprocess.PIPE
更改为
sys.stdout
并且当我在视图中启动子流程时它按预期工作所以它可能是模型中
subprocess.PIPE
的问题?

我应该在视图中启动流程还是有解决方案?

python django django-models subprocess
© www.soinside.com 2019 - 2024. All rights reserved.