• <bdo id='3O9mY'></bdo><ul id='3O9mY'></ul>
  • <tfoot id='3O9mY'></tfoot>
    <legend id='3O9mY'><style id='3O9mY'><dir id='3O9mY'><q id='3O9mY'></q></dir></style></legend>

      1. <small id='3O9mY'></small><noframes id='3O9mY'>

        <i id='3O9mY'><tr id='3O9mY'><dt id='3O9mY'><q id='3O9mY'><span id='3O9mY'><b id='3O9mY'><form id='3O9mY'><ins id='3O9mY'></ins><ul id='3O9mY'></ul><sub id='3O9mY'></sub></form><legend id='3O9mY'></legend><bdo id='3O9mY'><pre id='3O9mY'><center id='3O9mY'></center></pre></bdo></b><th id='3O9mY'></th></span></q></dt></tr></i><div id='3O9mY'><tfoot id='3O9mY'></tfoot><dl id='3O9mY'><fieldset id='3O9mY'></fieldset></dl></div>
      2. 用 Python 的 Popen 替换 Bash 风格的进程

        Bash style process substitution with Python#39;s Popen(用 Python 的 Popen 替换 Bash 风格的进程)
          <tbody id='LK5QQ'></tbody>
        <i id='LK5QQ'><tr id='LK5QQ'><dt id='LK5QQ'><q id='LK5QQ'><span id='LK5QQ'><b id='LK5QQ'><form id='LK5QQ'><ins id='LK5QQ'></ins><ul id='LK5QQ'></ul><sub id='LK5QQ'></sub></form><legend id='LK5QQ'></legend><bdo id='LK5QQ'><pre id='LK5QQ'><center id='LK5QQ'></center></pre></bdo></b><th id='LK5QQ'></th></span></q></dt></tr></i><div id='LK5QQ'><tfoot id='LK5QQ'></tfoot><dl id='LK5QQ'><fieldset id='LK5QQ'></fieldset></dl></div>

            1. <small id='LK5QQ'></small><noframes id='LK5QQ'>

              <tfoot id='LK5QQ'></tfoot>
            2. <legend id='LK5QQ'><style id='LK5QQ'><dir id='LK5QQ'><q id='LK5QQ'></q></dir></style></legend>
              • <bdo id='LK5QQ'></bdo><ul id='LK5QQ'></ul>

                • 本文介绍了用 Python 的 Popen 替换 Bash 风格的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Bash 中,您可以轻松地将进程的输出重定向到临时文件描述符,并且所有这些都由 bash 自动处理,如下所示:

                  In Bash you can easily redirect the output of a process to a temporary file descriptor and it is all automagically handled by bash like this:

                  $ mydaemon --config-file <(echo "autostart: True 
                   daemonize: True")
                  

                  或者像这样:

                  $ wc -l <(ls)
                  15 /dev/fd/63
                  

                  看看它怎么不是标准输入重定向:

                  see how it is not stdin redirection:

                  $ vim <(echo "Hello World") 
                  vim opens a text file containing "Hello world"
                  $ echo  "Hello World" | vim
                  Vim: Warning: Input is not from a terminal
                  

                  您可以在第二个示例中看到 bash 如何自动创建文件描述符并允许您将程序的输出传递给另一个程序.

                  You can see in the second example how bash automatically creates a file descriptor and allows you to pass the output of a program to another program.

                  现在我的问题是:我怎样才能用 Python 做同样的事情,在 subprocess 模块中使用 Popen?

                  Now onto my question: How can I do the same thing with Python, using Popen in the subprocess module?

                  我一直在使用普通的 kmers 文件并将其读入,但我的程序现在根据用户参数在运行时生成一个特定的 kmers 列表.我想避免手动写入临时文件,因为处理文件权限可能会给我的原始用户带来问题.

                  I have been using a normal file of kmers and just reading it in, but my program now generates a specific list of kmers at runtime based on user parameters. I'd like to avoid writing to a temporary file manually because dealing with file permissions could cause problems for my primitive users.

                  这是我运行程序并使用实际文件kmer_file"捕获标准输出的代码

                  Here is my code to run my program and capture the stdout with an actual file "kmer_file"

                  input_file = Popen(["pram_axdnull", str(kmer), input_file, kmer_file], stdout=PIPE)
                  

                  我创建了一个名为 generate_kmers 的函数,它返回一个可以轻松写入文件(包括换行符)或 StringIO 的字符串.我也有一个独立的 python 脚本来做同样的事情

                  I created a function called generate_kmers which returns a string that can be written out to a file easily (includes newlines) or to a StringIO. I also have a python script that is standalone to do the same thing

                  所以现在我想将它作为我的第三个参数传入:

                  So now I want to pass it in as my 3rd parameter:

                  这不起作用:

                  kmer_file = stringIO(generate_kmers(3))
                  input_file = Popen(["pram_axdnull", str(kmer), input_file, kmer_file], stdout=PIPE)
                  

                  这也不是:

                  kmer_file = Popen(["generate_kmers", str(kmer)], stdout=PIPE)
                  input_file = Popen(["pram_axdnull", str(kmer), input_file, kmer_file.stdout], stdout=PIPE)
                  

                  所以我没有想法.

                  有人知道解决这个问题的好方法吗?我正在考虑使用 shell=True 选项并使用 <() 的实际bashism,但我还没有弄清楚.

                  Does anyone know of a good way to resolve this? I was thinking using the shell=True option and using the actual bashism of <() but I haven't figured that out.

                  谢谢!

                  推荐答案

                  如果 pram_axdnull"-" 约定理解为:从标准输入读取",那么您可以:

                  If pram_axdnull understands "-" convention to mean: "read from stdin" then you could:

                  p = Popen(["pram_axdnull", str(kmer), input_filename, "-"],
                            stdin=PIPE, stdout=PIPE)
                  output = p.communicate(generate_kmers(3))[0]
                  

                  如果输入是由外部进程生成的:

                  If the input is generated by external process:

                  kmer_proc = Popen(["generate_kmers", str(kmer)], stdout=PIPE)
                  p = Popen(["pram_axdnull", str(kmer), input_filename, "-"],
                            stdin=kmer_proc.stdout, stdout=PIPE)
                  kmer_proc.stdout.close()
                  output = p.communicate()[0]
                  

                  如果 pram_axdnull 理解 "-" 约定:

                  If pram_axdnull doesn't understand "-" convention:

                  import os
                  import tempfile
                  from subprocess import check_output
                  
                  with tempfile.NamedTemporaryFile() as file:
                      file.write(generate_kmers(3))
                      file.delete = False
                  
                  try:
                      p = Popen(["pram_axdnull", str(kmer), input_filename, file.name],
                                stdout=PIPE)
                      output = p.communicate()[0]
                      # or
                      # output = check_output(["pram_axdnull", str(kmer), input_filename, 
                                               file.name])
                  finally:
                      os.remove(file.name)
                  

                  使用外部进程生成临时文件:

                  To generate temporary file using external process:

                  from subprocess import check_call
                  
                  with tempfile.NamedTemporaryFile() as file:
                      check_call(["generate_kmers", str(kmer)], stdout=file)
                      file.delete = False
                  

                  为避免等待所有 kmers 生成,即同时写入/读取 kmers,您可以在 Unix 上使用 os.mkfifo()(@cdarke 建议):

                  To avoid waiting for all kmers to be generated i.e., to write/read kmers simultaneously, you could use os.mkfifo() on Unix (suggested by @cdarke):

                  import os
                  import shutil
                  import tempfile
                  from contextlib import contextmanager
                  from subprocess import Popen, PIPE
                  
                  @contextmanager
                  def named_pipe():
                      dirname = tempfile.mkdtemp()
                      try:
                          path = os.path.join(dirname, 'named_pipe')
                          os.mkfifo(path)
                          yield path
                      finally:
                          shutil.rmtree(dirname)
                  
                  with named_pipe() as path:
                      p = Popen(["pram_axdnull", str(kmer), input_filename, path],
                                stdout=PIPE) # read from path
                      with open(path, 'wb') as wpipe:
                          kmer_proc = Popen(["generate_kmers", str(kmer)],
                                            stdout=wpipe) # write to path
                      output = p.communicate()[0]
                      kmer_proc.wait()
                  

                  这篇关于用 Python 的 Popen 替换 Bash 风格的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                    1. <i id='QjqLo'><tr id='QjqLo'><dt id='QjqLo'><q id='QjqLo'><span id='QjqLo'><b id='QjqLo'><form id='QjqLo'><ins id='QjqLo'></ins><ul id='QjqLo'></ul><sub id='QjqLo'></sub></form><legend id='QjqLo'></legend><bdo id='QjqLo'><pre id='QjqLo'><center id='QjqLo'></center></pre></bdo></b><th id='QjqLo'></th></span></q></dt></tr></i><div id='QjqLo'><tfoot id='QjqLo'></tfoot><dl id='QjqLo'><fieldset id='QjqLo'></fieldset></dl></div>
                      <tfoot id='QjqLo'></tfoot>
                      <legend id='QjqLo'><style id='QjqLo'><dir id='QjqLo'><q id='QjqLo'></q></dir></style></legend>

                      <small id='QjqLo'></small><noframes id='QjqLo'>

                          <tbody id='QjqLo'></tbody>

                        • <bdo id='QjqLo'></bdo><ul id='QjqLo'></ul>