Python script to start mondod

584 views
Skip to first unread message

Yaroslav Kyrpych

unread,
Feb 22, 2014, 7:27:45 PM2/22/14
to mongod...@googlegroups.com
Hello!

I am writing script to start mondod instance from Python. I want to use subprocess.call from Python library. But I am not entirely clear what needs to be passed as arguments. I realize that there should be port specified. Anything else, like path? Any example would be extremely helpful.

Thank you,

Yaroslav

Asya Kamsky

unread,
Feb 22, 2014, 8:17:43 PM2/22/14
to mongodb-user


--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Bernie Hackett

unread,
Feb 22, 2014, 9:46:28 PM2/22/14
to mongod...@googlegroups.com

Yaroslav Kyrpych

unread,
Feb 23, 2014, 6:19:43 PM2/23/14
to mongod...@googlegroups.com
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

Yaroslav Kyrpych

unread,
Feb 23, 2014, 6:20:46 PM2/23/14
to mongod...@googlegroups.com
I found the following that I am trying to re-use:

However, I am confused about arguments used in call. 

Yaroslav Kyrpych

unread,
Feb 23, 2014, 6:34:40 PM2/23/14
to mongod...@googlegroups.com
I am getting error message - no module pymongo.errors and from pymongo.read_preferences import ReadPreference.


On Sat, Feb 22, 2014 at 8:46 PM, Bernie Hackett <ber...@mongodb.com> wrote:

Stephen Steneker

unread,
Feb 23, 2014, 7:04:36 PM2/23/14
to mongod...@googlegroups.com
On Monday, 24 February 2014 10:34:40 UTC+11, Yaroslav Kyrpych wrote:
I am getting error message - no module pymongo.errors and from pymongo.read_preferences import ReadPreference.

Hi Yaroslav,

What version of PyMongo are you using? I believe pymongo.read_preferences was added in 2.3, but pymongo.errors has been around for much longer.

The current production version of PyMongo is 2.6.3, so you may need to update.

Regards,
Stephen

Yaroslav Kyrpych

unread,
Feb 23, 2014, 7:10:20 PM2/23/14
to mongod...@googlegroups.com
I am using 2.4.6 on Win 64. So, it should be there.


--

Yaroslav Kyrpych

unread,
Feb 23, 2014, 7:12:01 PM2/23/14
to mongod...@googlegroups.com
Another question I have - What do I pass as proc in wait_for function? Also port_num is not defined anywhere, do I pass 27017?

Yaroslav Kyrpych

unread,
Feb 23, 2014, 7:13:16 PM2/23/14
to mongod...@googlegroups.com
There variable port that has 27017, is it just typo when later there is reference to port_num?

Yaroslav Kyrpych

unread,
Feb 23, 2014, 7:20:02 PM2/23/14
to mongod...@googlegroups.com
One more question - Is this sufficient for cmd?
cmd=[mongod,'--dbpath','C:\mongodb\bin','--port',str(27017)]?

Asya Kamsky

unread,
Feb 24, 2014, 9:49:26 PM2/24/14
to mongodb-user
It would be sufficient, but I find it very unusual that your data
files are in c:\mongodb\bin - are you sure that's what you intended?
Port 27017 is the default so you shouldn't need to specify it
explicitly.

Asya


On Sun, Feb 23, 2014 at 4:20 PM, Yaroslav Kyrpych

Yaroslav Kyrpych

unread,
Feb 24, 2014, 11:47:29 PM2/24/14
to mongod...@googlegroups.com
Sorry, I was tired and not thinking straight. The data is in the following folder: C:\RCI\data. Thanks for pointing out mistake!

I am still wondering why I am having issues with pymongo.errors and pymongo.read_preferences. Any ideas/suggestions?

A. Jesse Jiryu Davis

unread,
Feb 27, 2014, 9:39:59 AM2/27/14
to mongod...@googlegroups.com
Could you please open the Python interpreter? Just type "Python" at the command prompt. Do:

>>> import pymongo
>>> print pymongo.version
>>> import pymongo.errors
>>> pymongo.read_preferences

Let us know what the output is. Thanks.

Yaroslav Kyrpych

unread,
Feb 28, 2014, 1:49:28 AM2/28/14
to mongod...@googlegroups.com
Typing "Python" gives error: NameError: name "Python" is not defined
print pymongo.version - wrong syntax, in python print has (), if I try it, getting AttributeError.
import pymongo.errors - ImportError: no module
pymongo.read_preferences - AttributeError, no attribute read_preferences.

Yaroslav Kyrpych

unread,
Feb 28, 2014, 1:51:01 AM2/28/14
to mongod...@googlegroups.com
I am going to re-install mongodb - hope this helps.

Jorge Puente Sarrín

unread,
Feb 28, 2014, 2:00:01 AM2/28/14
to mongod...@googlegroups.com
Hi Yaroslav,

In order to verify whatever MongoDB is successfully installed in your localhost, just run the mongo shell.

I don't understand why you have problems importing pymongo module. If your goal is run a MongoDB instance using Python's subprocess module, you don't need to use nor to install pymongo. Maybe... do you want to try connect to MongoDB after call to subprocess using the PyMongo driver?

Best regards.
Jorge Puente Sarrín.

Yaroslav Kyrpych

unread,
Feb 28, 2014, 1:34:40 PM2/28/14
to mongod...@googlegroups.com
Hi Jorge,

Mongo shell works find - I created manually database and inserted data there, and then used find() to display it. 

Yes, I would like to connect to MongoDB using PyMongo driver once the instance is up and running.

Thank you,

Yaroslav

Jorge Puente Sarrín

unread,
Feb 28, 2014, 1:42:27 PM2/28/14
to mongod...@googlegroups.com
Could you provide a snippet of your script?

I think you just need create an instance of MongoClient class.

Yaroslav Kyrpych

unread,
Mar 12, 2014, 1:59:13 PM3/12/14
to mongod...@googlegroups.com
Please see below straw-man (there are things that are wrong there) that I have at this time:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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

dbpath="C:\mongodb\bin"
default_logpath="C:\RCI\log"
hostname=os.environ.get('HOSTNAME',socket.gethostname())
port=int(os.environ.get('DBPORT',27017))
mongod=os.environ.get('MONGOD','mongod')

try:
from subprocess import DEVNULL
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):
return subprocess.Popen(cmd,stdout=DEVNULL,stderr=DEVNULL)
cmd=[mongod,'--dbpath','C:\mongodb\bin','--port',str(27017)]

proc=subprocess.Popen(cmd,stdout=DEVNULL,stderr=DEVNULL)
port_num="27017"
wait_for(proc,port_num)

Yaroslav Kyrpych

unread,
Mar 13, 2014, 2:09:47 PM3/13/14
to mongod...@googlegroups.com
I tried to run the code after cleaning it a bit. However, got error from Python:
File "C:\Python33\lib\subprocess.py", line 1112, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

Any ideas/suggestions are much appreciated. It looks like I don't have subprocess module?

Yaroslav Kyrpych

unread,
Mar 13, 2014, 2:13:39 PM3/13/14
to mongod...@googlegroups.com
I just checked Python installation - I do have subprocess.py at the specified path.
Reply all
Reply to author
Forward
0 new messages