FSX / momoko

Wraps (asynchronous) Psycopg2 for Tornado.
http://momoko.61924.nl/
Other
363 stars 73 forks source link

Tornado exits unexpectedly when connecting to database via momoko #114

Closed kolanos closed 9 years ago

kolanos commented 9 years ago

Thank you for putting this module together. When I attempt to connect to my local database, the script exits unexpectedly. Here's the code I'm using:

import logging
import momoko

from tornado import httpserver
from tornado import ioloop
from tornado import web

momoko_log = logging.getLogger('momoko')
momoko_log.setLevel(logging.DEBUG)
momoko_log.addHandler(logging.StreamHandler())

class IndexHandler(web.RequestHandler):
    def get(self):
        self.write('hello')

def main():
    app = web.Application([(r'/', IndexHandler)], debug=True)

    http_server = httpserver.HTTPServer(app)
    http_server.listen(8000)
    io_loop = ioloop.IOLoop.instance()

    app.db momoko.Pool(
        dsn='dbname=postgres user= postgres host=localhost port=5432',
        ioloop=io_loop)
    io_loop.add_future(app.db.connect(), lambda f: io_loop.stop())
    io_loop.start()

if __name__ == "__main__":
         main()

And here's the log messages:

Spawning new connection
Handling free connection 9
DEBUG:momoko:Handling free connection 9
No outstanding requests - adding to free pool
DEBUG:momoko:No outstanding requests - adding to free pool
All initial connection requests complete
DEBUG:momoko:All initial connection requests complete

And then it exits. What am I doing wrong?

haizaar commented 9 years ago

Well, it does exactly what you ask it for - once connect method finishes you ask it to call ioloop.stop(), which stops the event loop and the program finishes.

What are you trying to achieve exactly? On Aug 7, 2015 21:46, "Michael Lavers" notifications@github.com wrote:

Thank you for putting this module together. When I attempt to connect to my local database, the script exits unexpectedly. Here's the code I'm using:

import logging import momoko

from tornado import httpserver from tornado import ioloop from tornado import web

momoko_log = logging.getLogger('momoko') momoko_log.setLevel(logging.DEBUG) momoko_log.addHandler(logging.StreamHandler())

class IndexHandler(web.RequestHandler): def get(self): self.write('hello')

def main(): app = web.Application([(r'/', IndexHandler)], debug=True)

http_server = httpserver.HTTPServer(app)
http_server.listen(8000)
io_loop = ioloop.IOLoop.instance()

app.db momoko.Pool(
    dsn='dbname=postgres user= postgres host=localhost port=5432',
    ioloop=io_loop)
io_loop.add_future(app.db.connect(), lambda f: io_loop.stop())
io_loop.start()

if name == "main": main()

And here's the log messages:

Spawning new connection Handling free connection 9 DEBUG:momoko:Handling free connection 9 No outstanding requests - adding to free pool DEBUG:momoko:No outstanding requests - adding to free pool All initial connection requests complete DEBUG:momoko:All initial connection requests complete

And then it exits. What am I doing wrong?

— Reply to this email directly or view it on GitHub https://github.com/FSX/momoko/issues/114.

kolanos commented 9 years ago

Ok, that makes sense. So What I want it to do is keep the ioloop running so that the Tornado service can accept HTTP requests. I was using the example code as a reference. What should I be changing about the above? How do I establish a connection to Postgres without stopping the ioloop?

kolanos commented 9 years ago

Just re-reviewed the example code int he docs and noticed the second ioloop.start().