Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django-子进程调用工作不正常_Python_Django - Fatal编程技术网

Python Django-子进程调用工作不正常

Python Django-子进程调用工作不正常,python,django,Python,Django,我有以下意见: class MiniView(generic.DetailView): model = Automata template_name = 'convert/mini.html' context_object_name = 'automata' command = "python MiniDFA.py" subprocess.call(command, shell=True) class DFAView(generic.DetailView

我有以下意见:

class MiniView(generic.DetailView):
    model = Automata
    template_name = 'convert/mini.html'
    context_object_name = 'automata'
    command = "python MiniDFA.py"
    subprocess.call(command, shell=True)

class DFAView(generic.DetailView):
    model = Automata
    template_name = 'convert/dfa.html'
    context_object_name = 'automata'
    command = "python NFAtoDFA.py"
    subprocess.call(command, shell=True)

class TransitionCreate(UpdateView):
    model = Automata
    fields = []
    [...]
    command = "python make_graph.py"
    subprocess.call(command, shell=True)
    [...]
因为我希望每次调用视图时都能执行这些命令,但由于某些原因,只有最后一个命令似乎工作正常,我不知道为什么。。脚本本身似乎运行良好


我还注意到,每次服务器运行时,脚本都会执行,不知道为什么。

这些是类,不是函数。如果希望在每个请求上都发生一些事情,则需要重写类中的一个相关函数。例如:

class MiniView(generic.DetailView):
    ...

    def get(self, request):
        ...
        command = "python make_graph.py"
        subprocess.call(command, shell=True)
        ...
        return HttpResponse(...)
作为两项说明:

  • 在这里使用shell=True可能是个坏主意。如果可能的话,不要使用shell,当不需要复杂性时,它会增加复杂性
  • 如果shell脚本花费的时间太长,web客户端将超时。或者,用户将通过不断单击“重新加载”来重击服务器

    • 这些是类,不是函数。如果希望在每个请求上都发生一些事情,则需要重写类中的一个相关函数。例如:

      class MiniView(generic.DetailView):
          ...
      
          def get(self, request):
              ...
              command = "python make_graph.py"
              subprocess.call(command, shell=True)
              ...
              return HttpResponse(...)
      
      作为两项说明:

      • 在这里使用shell=True可能是个坏主意。如果可能的话,不要使用shell,当不需要复杂性时,它会增加复杂性
      • 如果shell脚本花费的时间太长,web客户端将超时。或者,用户将通过不断单击“重新加载”来重击服务器

      请参阅此相关问题;见此相关问题,;如果我不需要脚本返回任何东西,我只需要它运行,这仍然有效吗?仅此而已。您希望脚本何时运行?如果您希望它运行以响应HTTP请求,则需要返回一些内容(可能是HTTP)以响应显示给用户。如果我不需要脚本返回任何内容,我只需要它运行即可,这仍然有效吗?仅此而已。您希望脚本何时运行?如果希望它运行以响应HTTP请求,则需要返回一些内容(可能是HTTP)以响应向用户显示的内容。