Skip to content

Instantly share code, notes, and snippets.

@brentalanmiller
Created May 8, 2015 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentalanmiller/efb184b495cdb92d4524 to your computer and use it in GitHub Desktop.
Save brentalanmiller/efb184b495cdb92d4524 to your computer and use it in GitHub Desktop.
MongoDB Motor Deadlock
import json
import random
import motor
from tornado import gen, ioloop, web
@gen.coroutine
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):
@gen.coroutine
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()
self.write("done")
class InsertHandler(web.RequestHandler):
@gen.coroutine
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)
self.write("done")
if __name__ == "__main__":
#http://code.activestate.com/recipes/577334-how-to-debug-deadlocked-multi-threaded-programs/
import stacktracer
stacktracer.trace_start("trace.html")
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)],
**settings)
application.listen(8888)
ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment