Thank you! That's much more than I needed. At this point I am not trying to have HA. If I were to simplify your code, does the following make sense:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import os
import random
import shutil
import signal
import socket
import subprocess
import sys
import time
from stat import S_IRUSR
import pymongo
import pymongo.errors
from pymongo.read_preferences import ReadPreference
home = os.environ.get('HOME')
default_dbpath = os.path.join(home, 'data', 'pymongo_high_availability')
dbpath = os.environ.get('DBPATH', default_dbpath)
default_logpath = os.path.join(home, 'log', 'pymongo_high_availability')
logpath = os.environ.get('LOGPATH', default_logpath)
hostname = os.environ.get('HOSTNAME', socket.gethostname())
port = int(os.environ.get('DBPORT', 27017))
mongod = os.environ.get('MONGOD', 'mongod')
mongos = os.environ.get('MONGOS', 'mongos')
try:
from subprocess import DEVNULL # Python 3.
except ImportError:
DEVNULL = open(os.devnull, 'wb')
def wait_for(proc, port_num):
trys = 0
while proc.poll() is None and trys < 160:
trys += 1
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
try:
s.connect((hostname, port_num))
return True
except (IOError, socket.error):
time.sleep(0.25)
finally:
s.close()
def start_subprocess(cmd):
"""Run cmd (a list of strings) and return a Popen instance."""
return subprocess.Popen(cmd, stdout=DEVNULL, stderr=DEVNULL)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Do I need to use any other functions? I am confused about cmd, I could not find good explanation online of what goes there.
Thank you a lot for helping!
Yaroslav