Search code examples
pythonlinuxpython-multithreading

execute multiple commands in Linux using python in the same time


I need to execute multiple commands in Linux using python in the same time. I don't need to run it command by command.

I try to write this code but i can't understand how to execute multiple commands in the same time using python also i read about python multithreading but i don't know how to use it.

Code:

#  -*- coding: utf-8 -*-

import os

commands = ['ping www.google.com', 'ping www.yahoo.com', 'ping www.hotmail.com']
count = 0
for com in commands:
    print "Start execute commands.."
    os.system(com)
    count += 1
    print "[OK] command "+str(count)+" runing successfully."
else:
    print "Finish.."

Please how i can do that with python and execute multiple commands in the same time??


Solution

  • Looks like a typical producer-consumer problem

    import threading
    import os
    
    commands = ['ping www.google.com', 'ping www.yahoo.com', 'ping www.hotmail.com']
    
    def worker_func():
        while commands:   # Checks if the list is not-empty. Loop exits when list is becomes empty
            com = commands.pop(0)
            print "Start execute commands.."
            os.system(com)
            count += 1
            print "[OK] command "+str(count)+" runing successfully."
            
    workers = [threading.Thread(target=worker_func, args=tuple(), name='thread_'+str(i))  for i in range(5) ]  # Create 5 workers (consumers)
    [worker.start() for worker in workers]  # Start working
    [worker.join() for worker in workers]   # Wait for all workers to finish
    

    Here I have created the 5 worker threads. These threads will run function worker_func.
    worker_func Will pick up one element from the list and preform the job. When list becomes empty the function returns (exits).

    Note: Read about Global Interpreter Lock to understand where python multithreading should not be used.
    In this case the GIL (Global Interpreter Lock) should not affect you because the worker_func call a subprocess and wait for it complete. While the thread is waiting GIL is released to other threads.