Last active
December 14, 2015 19:18
-
-
Save soundkitchen/5135181 to your computer and use it in GitHub Desktop.
meinheld.patch#patch_socket した時に mongoengine? pymongo がエラーを吐くのを回避するには、コネクト用関数を用意して meinheld.server#spawn に渡すと良いっぽい :-)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim: fileencoding=utf-8 : | |
import logging | |
from mongoengine import * | |
__all__ = ('connect_to_mongodb', 'AccessLog',) | |
def connect_to_mongodb(): | |
logging.info("connect to mongodb") | |
connect("spam") | |
class AccessLog(Document): | |
time = FloatField(required=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim: fileencoding=utf-8 : | |
import time | |
import logging | |
from meinheld import ( | |
server, patch, | |
) | |
from lib import * | |
patch.patch_socket() | |
logging.basicConfig(level=logging.DEBUG) | |
def app(environ, start_response): | |
# store new object. | |
AccessLog(time=time.time()).save() | |
# retrieve all objects. | |
logging.info("log count: %s", len(AccessLog.objects)) | |
# create response. | |
body = "hello world." | |
start_response("200 OK", [ | |
('Content-Type', 'text/plain; charset=utf-8'), | |
('Content-Length', str(len(body))), | |
]) | |
return [body] | |
if __name__ == '__main__': | |
server.spawn(connect_to_mongodb) | |
server.listen(('0.0.0.0', 8000)) | |
server.run(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> from lib import * | |
>>> AccessLog.objects |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment