Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
MongoDB Motor Deadlock
import json
import random
import motor
from tornado import gen, ioloop, web
def get_data():
cur1 = _client.test.testcol1.find()
cur2 = _client.test.testcol2.find()
data1, data2 = yield [cur1.to_list(length=None), cur2.to_list(length=None)]
raise gen.Return((data1, data2))
class MainHandler(web.RequestHandler):
def get(self):
#If I run the code directly in this method this it works fine
# cur1 = _client.test.testcol1.find()
# cur2 = _client.test.testcol2.find()
# data1, data2 = yield [cur1.to_list(length=None), cur2.to_list(length=None)]
yield get_data()
class InsertHandler(web.RequestHandler):
def get(self):
for _ in range(1000):
docs = []
for _ in range(1000):
docs.append({"v": random.random(), "t": random.randint(0, 2**31-1)})
yield _client.test.testcol1.insert(docs)
yield _client.test.testcol2.insert(docs)
if __name__ == "__main__":
import stacktracer
settings = {
"debug": True,
"autoreload": True
with open("credentials.json") as f:
creds = json.load(f)
_client = motor.MotorReplicaSetClient(creds["mongodb_uri"])
application = web.Application([(r"/", MainHandler),
(r"/doinsert", InsertHandler)],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment