0

I am using python script with multiprocessing module to run 2 processes at the same time. Each process takes 4-5 minutes to complete. I am not able to run both process simultaneously. Below is my script.

My script run on server C and it takes two arguments as IP addresses and takes MTR from server A & B. A---->B & B----->A. This should happen at the same time.

 import paramiko
 import os
 import argparse
 from multiprocessing import Process, Queue, Lock
 parser = argparse.ArgumentParser()
 parser.add_argument("ip1", help="Input the Source IP Address: ")
 parser.add_argument("ip2", help="Input the Destination IP Address: ")
 args = parser.parse_args()
 class Myssh:
 def __init__(self, iph, ipd):
 self.iph = iph
 self.ipd = ipd
 self.cmd = "mtr --no-dns -rwc 300 %s" % ipd
 def do(self):
 count = 1
 print "Starting Thread"
 client = paramiko.SSHClient()
 client.set_missing_host_key_policy(paramiko.WarningPolicy())
 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 key = os.path.expanduser(r'/home/rsharma/master004_rsa')
 privkey = paramiko.RSAKey.from_private_key_file(key)
 client.connect(self.iph, port=12001, username="master", pkey=privkey, look_for_keys=False)
 stdin, stdout, stderr = client.exec_command(self.cmd)
 for line in stdout:
 print '...' + line.strip('\n')
 client.close()
 if __name__ == "__main__":
 lock = Lock()
 s1 = Myssh(args.ip1, args.ip2)
 s2 = Myssh(args.ip2, args.ip1)
 p1 = Process(target=s1.do())
 p1.start()
 p2 = Process(target=s2.do())
 p2.start()
 p1.join()
 p2.join()
Vincent Savard
36.1k10 gold badges71 silver badges73 bronze badges
asked Mar 31, 2016 at 11:47
2
  • Right now, my script is executing both processes sequentially not in parallel. I want it to run both commands in parallel and complete in 5 minutes inspite of 10 minutes. Commented Mar 31, 2016 at 11:49
  • @timlyo If you edit indentation (and you really shouldn't in most of the cases), at least make sure you don't change the intent of the code. It's a shame this was approved by reviewers who pay no attention. Commented Mar 31, 2016 at 12:50

1 Answer 1

2

This code is saying run the function, and set what it returns as the process target. You need to change these lines

p1 = Process(target=s1.do())
p2 = Process(target=s2.do())

to

p1 = Process(target=s1.do)
p2 = Process(target=s2.do)

The brackets after the function are running it there and then.

answered Mar 31, 2016 at 12:10
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I am not getting any error due to indentation. Please clarify your comment on indentation in my script.
The error isn't from the indentation. I was simply trying to format the code in your answer so that it looked better. You need to remove the brackets after the s1.do in your code. See my answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.