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

      <tfoot id='D4Lcw'></tfoot>

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

      1. <legend id='D4Lcw'><style id='D4Lcw'><dir id='D4Lcw'><q id='D4Lcw'></q></dir></style></legend>
        <i id='D4Lcw'><tr id='D4Lcw'><dt id='D4Lcw'><q id='D4Lcw'><span id='D4Lcw'><b id='D4Lcw'><form id='D4Lcw'><ins id='D4Lcw'></ins><ul id='D4Lcw'></ul><sub id='D4Lcw'></sub></form><legend id='D4Lcw'></legend><bdo id='D4Lcw'><pre id='D4Lcw'><center id='D4Lcw'></center></pre></bdo></b><th id='D4Lcw'></th></span></q></dt></tr></i><div id='D4Lcw'><tfoot id='D4Lcw'></tfoot><dl id='D4Lcw'><fieldset id='D4Lcw'></fieldset></dl></div>

        Python:并行执行 cat 子进程

        Python: execute cat subprocess in parallel(Python:并行执行 cat 子进程)
          • <bdo id='qkjIG'></bdo><ul id='qkjIG'></ul>

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

              <tbody id='qkjIG'></tbody>

              <legend id='qkjIG'><style id='qkjIG'><dir id='qkjIG'><q id='qkjIG'></q></dir></style></legend>
              • <tfoot id='qkjIG'></tfoot>

                • <i id='qkjIG'><tr id='qkjIG'><dt id='qkjIG'><q id='qkjIG'><span id='qkjIG'><b id='qkjIG'><form id='qkjIG'><ins id='qkjIG'></ins><ul id='qkjIG'></ul><sub id='qkjIG'></sub></form><legend id='qkjIG'></legend><bdo id='qkjIG'><pre id='qkjIG'><center id='qkjIG'></center></pre></bdo></b><th id='qkjIG'></th></span></q></dt></tr></i><div id='qkjIG'><tfoot id='qkjIG'></tfoot><dl id='qkjIG'><fieldset id='qkjIG'></fieldset></dl></div>
                  本文介绍了Python:并行执行 cat 子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在运行几个 cat |远程服务器上的 zgrep 命令并单独收集其输出以进行进一步处理:

                  I am running several cat | zgrep commands on a remote server and gathering their output individually for further processing:

                  class MainProcessor(mp.Process):
                      def __init__(self, peaks_array):
                          super(MainProcessor, self).__init__()
                          self.peaks_array = peaks_array
                  
                      def run(self):
                          for peak_arr in self.peaks_array:
                              peak_processor = PeakProcessor(peak_arr)
                              peak_processor.start()
                  
                  class PeakProcessor(mp.Process):
                      def __init__(self, peak_arr):
                          super(PeakProcessor, self).__init__()
                          self.peak_arr = peak_arr
                  
                      def run(self):
                          command = 'ssh remote_host cat files_to_process | zgrep --mmap "regex" '
                          log_lines = (subprocess.check_output(command, shell=True)).split('
                  ')
                          process_data(log_lines)
                  

                  然而,这会导致 subprocess('ssh ... cat ...') 命令的顺序执行.第二个高峰等待第一个完成,依此类推.

                  This, however, results in sequential execution of the subprocess('ssh ... cat ...') commands. Second peak waits for first to finish and so on.

                  如何修改此代码以使子进程调用并行运行,同时仍能够单独收集每个子进程的输出?

                  How can I modify this code so that the subprocess calls run in parallel, while still being able to collect the output for each individually?

                  推荐答案

                  另一种方法(而不是其他将 shell 进程置于后台的建议)是使用 多线程.

                  Another approach (rather than the other suggestion of putting shell processes in the background) is to use multithreading.

                  您拥有的 run 方法会执行如下操作:

                  The run method that you have would then do something like this:

                  thread.start_new_thread ( myFuncThatDoesZGrep)
                  

                  要收集结果,您可以执行以下操作:

                  To collect results, you can do something like this:

                  class MyThread(threading.Thread):
                     def run(self):
                         self.finished = False
                         # Your code to run the command here.
                         blahBlah()
                         # When finished....
                         self.finished = True
                         self.results = []
                  

                  按照上面关于多线程的链接中的说明运行线程.当您的线程对象具有 myThread.finished == True 时,您可以通过 myThread.results 收集结果.

                  Run the thread as stated above in the link on multithreading. When your thread object has myThread.finished == True, then you can collect the results via myThread.results.

                  这篇关于Python:并行执行 cat 子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 画布滚动条?)
                  <legend id='5sy5W'><style id='5sy5W'><dir id='5sy5W'><q id='5sy5W'></q></dir></style></legend>
                • <i id='5sy5W'><tr id='5sy5W'><dt id='5sy5W'><q id='5sy5W'><span id='5sy5W'><b id='5sy5W'><form id='5sy5W'><ins id='5sy5W'></ins><ul id='5sy5W'></ul><sub id='5sy5W'></sub></form><legend id='5sy5W'></legend><bdo id='5sy5W'><pre id='5sy5W'><center id='5sy5W'></center></pre></bdo></b><th id='5sy5W'></th></span></q></dt></tr></i><div id='5sy5W'><tfoot id='5sy5W'></tfoot><dl id='5sy5W'><fieldset id='5sy5W'></fieldset></dl></div>

                    <bdo id='5sy5W'></bdo><ul id='5sy5W'></ul>

                          <tbody id='5sy5W'></tbody>

                            <tfoot id='5sy5W'></tfoot>

                            <small id='5sy5W'></small><noframes id='5sy5W'>