Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection examples (WIP) #217

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/connection/connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from twisted.internet import defer
from twisted.internet.task import react
from txmongo.connection import ConnectionPool


@defer.inlineCallbacks
def main(reactor):
# default pool_size is 1
mongo_conn = ConnectionPool("mongodb://localhost:27017/", pool_size=4)

yield mongo_conn.test.hobbits.insert({"name": "Frodo"})
yield mongo_conn.disconnect()


react(main)
27 changes: 27 additions & 0 deletions examples/connection/connect_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from twisted.internet import defer
from twisted.internet.task import react
from txmongo.connection import ConnectionPool


@defer.inlineCallbacks
def replicaset():
conn = ConnectionPool("mongodb://localhost:27017/?replicaSet=rs0")
yield conn.test.fruits.insert({"kind": "Pear"})
yield conn.disconnect()


@defer.inlineCallbacks
def read_preference():
conn = ConnectionPool("mongodb://localhost:27017/?readPreference=secondary")
fruits = yield conn.test.fruits.find()
print(fruits)
yield conn.disconnect()


@defer.inlineCallbacks
def main(reactor):
yield replicaset()
yield read_preference()


react(main)