Motorengine是否使用mongo db维护IO流? [英] Does Motorengine mantain an IO stream with mongo db?

查看:84
本文介绍了Motorengine是否使用mongo db维护IO流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Tornado应用程序中使用Motorengine.如 docs 中所述,这就是我应该制作的ODM

from motorengine.document import Document
from motorengine.fields import StringField, DateTimeField

class Article(Document):
    title = StringField(required=True)
    description = StringField(required=True)
    published_date = DateTimeField(auto_now_on_insert=True)

    new_title = "Better Title %s" % uuid4()

def create_article():
    Article.objects.create(
        title="Some Article",
        description="This is an article that really matters.",
        callback=handle_article_created
    )

def handle_article_created(article):
   article.title = new_title
   article.save(callback=handle_article_updated)

def handle_article_updated(article):
    Article.objects.find_all(callback=handle_articles_loaded)

def handle_articles_loaded(articles):
    assert len(articles) == 1
    assert articles[0].title == new_title
    articles[0].delete(callback=handle_article_deleted)

def handle_article_deleted(number_of_deleted_items):
    try:
        assert number_of_deleted_items == 1
    finally:
        io_loop.stop()

io_loop.add_timeout(1, create_article)
io_loop.start()

困惑是,一旦创建实例,这会与数据库保持一致的连接吗?我希望所有模型的数据库都进行非阻塞的I/O操作,所以如果这样做的话,这不是问题.但是我不应该为用户模型实现它,在验证时我将只访问一次.有访问用户数据的常规方法吗?

此外,我不清楚几件事:

  1. 最后两行(ioloop)在这里到底在做什么?

  2. 我在不同的文件中声明不同的模型,我是否每个文件都做一个io_loop.start()?这部分对我来说似乎很奇怪.

请帮帮我.谢谢.

https://github.com/heynemann/motorengine/blob/master/motorengine/connection.py#L62

每个MotorClient都维护一个IOStreams持久池,因此您的问题的答案是肯定的.

IOLoop.start启动您的应用程序服务器.您应该在一个文件中调用一次.对start的调用将一直运行,或者直到您终止该进程为止.

add_timeout调用只是演示MotorEngine功能的一种方法,它等待一秒钟,然后调用create_article.在您的实际应用程序中,您不会做任何类似的事情;您可能会直接在RequestHandler中调用create_article,而没有add_timeout.

I want to use Motorengine for my Tornado application. As given in the docs, this is how I am supposed to make the ODM

from motorengine.document import Document
from motorengine.fields import StringField, DateTimeField

class Article(Document):
    title = StringField(required=True)
    description = StringField(required=True)
    published_date = DateTimeField(auto_now_on_insert=True)

    new_title = "Better Title %s" % uuid4()

def create_article():
    Article.objects.create(
        title="Some Article",
        description="This is an article that really matters.",
        callback=handle_article_created
    )

def handle_article_created(article):
   article.title = new_title
   article.save(callback=handle_article_updated)

def handle_article_updated(article):
    Article.objects.find_all(callback=handle_articles_loaded)

def handle_articles_loaded(articles):
    assert len(articles) == 1
    assert articles[0].title == new_title
    articles[0].delete(callback=handle_article_deleted)

def handle_article_deleted(number_of_deleted_items):
    try:
        assert number_of_deleted_items == 1
    finally:
        io_loop.stop()

io_loop.add_timeout(1, create_article)
io_loop.start()

The confusion is, will this maintain a consistent connection with the database once an instance is created ? I want non-blocking I/O operations with the database for all the models so it's not a problem if it does. But I should not be implementing it for the Users model which I will access only once at the time of verification. Is there a normal way to access Users data ?

Also, I'm unclear about a few things :

  1. What exactly are the last two lines (ioloop) doing here?

  2. I am declaring different models in separate files, do I do a io_loop.start() is each file? This is the part that seems weird to me.

Please help me out. Thanks.

解决方案

Reading MotorEngine's code, it appears to me that each operation you do with MongoDB, like Article.objects.create, checks for a MotorClient instance and reuses one if possible. Here's where the connection is cached and looked up from the cache:

https://github.com/heynemann/motorengine/blob/master/motorengine/connection.py#L62

Each MotorClient maintains a persistent pool of IOStreams, so the answer to your question is yes.

IOLoop.start starts your application server. You should call it once, in one file. The call to start runs forever, or until you kill the process.

The add_timeout call is just a way to demonstrate MotorEngine's functionality, it waits one second then calls create_article. You wouldn't do anything like that in your actual application; you'd likely call create_article directly in a RequestHandler, without add_timeout.

这篇关于Motorengine是否使用mongo db维护IO流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆