Multiple Processes - Python












0














I am looking to run multiple instances of a command line script at the same time. I am new to this concept of "multi-threading" so am at bit of a loss as to why I am seeing the things that I am seeing.



I have tried to execute the sub-processing in two different ways:



1 - Using multiple calls of Popen without a communicate until the end:



command = 'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum1 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum1{}'.format(i), str(i))

process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

command = 'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum2 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum2{}'.format(i), str(i))

process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

command = 'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum3 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum3{}'.format(i), str(i))

process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

(stdoutdata, stderrdata) = process.communicate()


this starts up each of the command line item but only completes the last entry leaving the other 2 hanging.



2 - Attempting to implement an example from Python threading multiple bash subprocesses? but nothing happens except for a printout of the commands (program hangs with no command line arguments running as observed in windows task manager:



import threading
import Queue
import commands
import time

workspace = r'F:ProcessingSM'
image = 't08r_e'
image_name = (image.split('.'))[0]
i = 0
process_image_tif = workspace + '\{}{}.tif'.format((image.split('r'))[0], str(i))

# thread class to run a command
class ExampleThread(threading.Thread):
def __init__(self, cmd, queue):
threading.Thread.__init__(self)
self.cmd = cmd
self.queue = queue

def run(self):
# execute the command, queue the result
(status, output) = commands.getstatusoutput(self.cmd)
self.queue.put((self.cmd, output, status))

# queue where results are placed
result_queue = Queue.Queue()

# define the commands to be run in parallel, run them
cmds = ['raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum1 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum1{}'.format(i), str(i)),
'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum2 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum2{}'.format(i), str(i)),
'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum3 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum3{}'.format(i), str(i)),

]
for cmd in cmds:
thread = ExampleThread(cmd, result_queue)
thread.start()

# print results as we get them
while threading.active_count() > 1 or not result_queue.empty():
while not result_queue.empty():
(cmd, output, status) = result_queue.get()
print(cmd)
print(output)


How can I run all of these commands at the same time achieving a result at the end? I am running in windows, pyhton 2.7.










share|improve this question



























    0














    I am looking to run multiple instances of a command line script at the same time. I am new to this concept of "multi-threading" so am at bit of a loss as to why I am seeing the things that I am seeing.



    I have tried to execute the sub-processing in two different ways:



    1 - Using multiple calls of Popen without a communicate until the end:



    command = 'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum1 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum1{}'.format(i), str(i))

    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

    command = 'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum2 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum2{}'.format(i), str(i))

    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

    command = 'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum3 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum3{}'.format(i), str(i))

    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

    (stdoutdata, stderrdata) = process.communicate()


    this starts up each of the command line item but only completes the last entry leaving the other 2 hanging.



    2 - Attempting to implement an example from Python threading multiple bash subprocesses? but nothing happens except for a printout of the commands (program hangs with no command line arguments running as observed in windows task manager:



    import threading
    import Queue
    import commands
    import time

    workspace = r'F:ProcessingSM'
    image = 't08r_e'
    image_name = (image.split('.'))[0]
    i = 0
    process_image_tif = workspace + '\{}{}.tif'.format((image.split('r'))[0], str(i))

    # thread class to run a command
    class ExampleThread(threading.Thread):
    def __init__(self, cmd, queue):
    threading.Thread.__init__(self)
    self.cmd = cmd
    self.queue = queue

    def run(self):
    # execute the command, queue the result
    (status, output) = commands.getstatusoutput(self.cmd)
    self.queue.put((self.cmd, output, status))

    # queue where results are placed
    result_queue = Queue.Queue()

    # define the commands to be run in parallel, run them
    cmds = ['raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum1 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum1{}'.format(i), str(i)),
    'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum2 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum2{}'.format(i), str(i)),
    'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum3 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum3{}'.format(i), str(i)),

    ]
    for cmd in cmds:
    thread = ExampleThread(cmd, result_queue)
    thread.start()

    # print results as we get them
    while threading.active_count() > 1 or not result_queue.empty():
    while not result_queue.empty():
    (cmd, output, status) = result_queue.get()
    print(cmd)
    print(output)


    How can I run all of these commands at the same time achieving a result at the end? I am running in windows, pyhton 2.7.










    share|improve this question

























      0












      0








      0







      I am looking to run multiple instances of a command line script at the same time. I am new to this concept of "multi-threading" so am at bit of a loss as to why I am seeing the things that I am seeing.



      I have tried to execute the sub-processing in two different ways:



      1 - Using multiple calls of Popen without a communicate until the end:



      command = 'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum1 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum1{}'.format(i), str(i))

      process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

      command = 'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum2 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum2{}'.format(i), str(i))

      process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

      command = 'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum3 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum3{}'.format(i), str(i))

      process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

      (stdoutdata, stderrdata) = process.communicate()


      this starts up each of the command line item but only completes the last entry leaving the other 2 hanging.



      2 - Attempting to implement an example from Python threading multiple bash subprocesses? but nothing happens except for a printout of the commands (program hangs with no command line arguments running as observed in windows task manager:



      import threading
      import Queue
      import commands
      import time

      workspace = r'F:ProcessingSM'
      image = 't08r_e'
      image_name = (image.split('.'))[0]
      i = 0
      process_image_tif = workspace + '\{}{}.tif'.format((image.split('r'))[0], str(i))

      # thread class to run a command
      class ExampleThread(threading.Thread):
      def __init__(self, cmd, queue):
      threading.Thread.__init__(self)
      self.cmd = cmd
      self.queue = queue

      def run(self):
      # execute the command, queue the result
      (status, output) = commands.getstatusoutput(self.cmd)
      self.queue.put((self.cmd, output, status))

      # queue where results are placed
      result_queue = Queue.Queue()

      # define the commands to be run in parallel, run them
      cmds = ['raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum1 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum1{}'.format(i), str(i)),
      'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum2 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum2{}'.format(i), str(i)),
      'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum3 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum3{}'.format(i), str(i)),

      ]
      for cmd in cmds:
      thread = ExampleThread(cmd, result_queue)
      thread.start()

      # print results as we get them
      while threading.active_count() > 1 or not result_queue.empty():
      while not result_queue.empty():
      (cmd, output, status) = result_queue.get()
      print(cmd)
      print(output)


      How can I run all of these commands at the same time achieving a result at the end? I am running in windows, pyhton 2.7.










      share|improve this question













      I am looking to run multiple instances of a command line script at the same time. I am new to this concept of "multi-threading" so am at bit of a loss as to why I am seeing the things that I am seeing.



      I have tried to execute the sub-processing in two different ways:



      1 - Using multiple calls of Popen without a communicate until the end:



      command = 'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum1 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum1{}'.format(i), str(i))

      process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

      command = 'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum2 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum2{}'.format(i), str(i))

      process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

      command = 'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum3 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum3{}'.format(i), str(i))

      process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

      (stdoutdata, stderrdata) = process.communicate()


      this starts up each of the command line item but only completes the last entry leaving the other 2 hanging.



      2 - Attempting to implement an example from Python threading multiple bash subprocesses? but nothing happens except for a printout of the commands (program hangs with no command line arguments running as observed in windows task manager:



      import threading
      import Queue
      import commands
      import time

      workspace = r'F:ProcessingSM'
      image = 't08r_e'
      image_name = (image.split('.'))[0]
      i = 0
      process_image_tif = workspace + '\{}{}.tif'.format((image.split('r'))[0], str(i))

      # thread class to run a command
      class ExampleThread(threading.Thread):
      def __init__(self, cmd, queue):
      threading.Thread.__init__(self)
      self.cmd = cmd
      self.queue = queue

      def run(self):
      # execute the command, queue the result
      (status, output) = commands.getstatusoutput(self.cmd)
      self.queue.put((self.cmd, output, status))

      # queue where results are placed
      result_queue = Queue.Queue()

      # define the commands to be run in parallel, run them
      cmds = ['raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum1 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum1{}'.format(i), str(i)),
      'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum2 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum2{}'.format(i), str(i)),
      'raster2pgsql -I -C -e -s 26911 %s -t 100x100 -F p839.%s_image_sum_sum3 | psql -U david -d projects -h pg3' % (workspace + '\r_sumsum3{}'.format(i), str(i)),

      ]
      for cmd in cmds:
      thread = ExampleThread(cmd, result_queue)
      thread.start()

      # print results as we get them
      while threading.active_count() > 1 or not result_queue.empty():
      while not result_queue.empty():
      (cmd, output, status) = result_queue.get()
      print(cmd)
      print(output)


      How can I run all of these commands at the same time achieving a result at the end? I am running in windows, pyhton 2.7.







      windows multithreading python-2.7






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 20:51









      David_C

      16511




      16511
























          1 Answer
          1






          active

          oldest

          votes


















          0














          My first try didn't work because of the repeated definitions of stdout and sterror. Removing these definitions causes expected behavior.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401331%2fmultiple-processes-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            My first try didn't work because of the repeated definitions of stdout and sterror. Removing these definitions causes expected behavior.






            share|improve this answer


























              0














              My first try didn't work because of the repeated definitions of stdout and sterror. Removing these definitions causes expected behavior.






              share|improve this answer
























                0












                0








                0






                My first try didn't work because of the repeated definitions of stdout and sterror. Removing these definitions causes expected behavior.






                share|improve this answer












                My first try didn't work because of the repeated definitions of stdout and sterror. Removing these definitions causes expected behavior.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 20:50









                David_C

                16511




                16511






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401331%2fmultiple-processes-python%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen