问题
I am building a small system which throws data from a mongodb collection, it already works fine but I have to restart it everytime I make changes. I already have a monitor that dectect changes and restarts the server automatically but I want to do something like this with mongodb changes. I am currenlty using CentOs 5, Nginx, uWsgi & python2.7.
回答1:
I'd look into using tailable cursors, which remain alive after they've reached the end of a collection, and can block until a new object is available.
Using PyMongo, you can call Collection.find
with a tailable=True
option to enable this behavior. This blog post gives some good examples of its usage.
Additionally, instead of just querying the collection, which will only alert you to new objects added to that collection, you may want to query the database's oplog, which is a collection of all insert, updates, and deletes called on any collection in the database. Note that replication must be enabled for mongo to keep an oplog. Check out this blog post for info about the oplog and enabling replication.
来源:https://stackoverflow.com/questions/13808886/with-python-is-there-a-way-to-listen-for-changes-when-insert-or-update-is-made-i