Skip to content

Instantly share code, notes, and snippets.

@soundkitchen
Last active December 14, 2015 19:18
Show Gist options
  • Save soundkitchen/5135181 to your computer and use it in GitHub Desktop.
Save soundkitchen/5135181 to your computer and use it in GitHub Desktop.
meinheld.patch#patch_socket した時に mongoengine? pymongo がエラーを吐くのを回避するには、コネクト用関数を用意して meinheld.server#spawn に渡すと良いっぽい :-)
# 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)
# 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)
>>> from lib import *
>>> AccessLog.objects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment