Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:17:14+00:00 2026-06-13T10:17:14+00:00

I am new to python and even newer to tornado/momoko. I am struggling with

  • 0

I am new to python and even newer to tornado/momoko. I am struggling with an example from momoko’s website. I have the database.cfg file configured with my settings.

#!/usr/bin/env python
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado import gen
import momoko
import settings

class BaseHandler(tornado.web.RequestHandler):
    @property
    def db(self):
        return self.application.db


class OverviewHandler(BaseHandler):
    def get(self):
        self.write('''
<ul>
<li><a href="/query">A single query</a></li>
<li><a href="/batch">A batch of queries</a></li>
<li><a href="/chain">A chain of queries</a></li>
<li><a href="/multi_query">Multiple queries executed with gen.Task</a></li>
<li><a href="/callback_and_wait">Multiple queries executed with gen.Callback and gen.Wait</a></li>
</ul>
''')
        self.finish()


class SingleQueryHandler(BaseHandler):
    @tornado.web.asynchronous
    @gen.engine
    def get(self):
        # One simple query
        cursor = yield gen.Task(self.db.execute, 'SELECT 42, 12, %s, 11;', (25,))
        self.write('Query results: %s' % cursor.fetchall())
        self.finish()


class BatchQueryHandler(BaseHandler):
    @tornado.web.asynchronous
    @gen.engine
    def get(self):
        # These queries are executed all at once and therefore they need to be
        # stored in an dictionary so you know where the resulting cursors
        # come from, because they won't arrive in the same order.
        cursors = yield gen.Task(self.db.batch, {
            'query1': ['SELECT 42, 12, %s, %s;', (23, 56)],
            'query2': 'SELECT 1, 2, 3, 4, 5;',
            'query3': 'SELECT 465767, 4567, 3454;'
        })

        for key, cursor in cursors.items():
            self.write('Query results: %s = %s<br>' % (key, cursor.fetchall()))
        self.finish()


class QueryChainHandler(BaseHandler):
    @tornado.web.asynchronous
    @gen.engine
    def get(self):
        # Execute a list of queries in the order you specified
        cursors = yield gen.Task(self.db.chain, (
            ['SELECT 42, 12, %s, 11;', (23,)],
            'SELECT 1, 2, 3, 4, 5;'
        ))

        for cursor in cursors:
            self.write('Query results: %s<br>' % cursor.fetchall())
        self.finish()


class MultiQueryHandler(BaseHandler):
    @tornado.web.asynchronous
    @gen.engine
    def get(self):
        cursor1, cursor2, cursor3 = yield [
            gen.Task(self.db.execute, 'SELECT 42, 12, %s, 11;', (25,)),
            gen.Task(self.db.execute, 'SELECT 42, 12, %s, %s;', (23, 56)),
            gen.Task(self.db.execute, 'SELECT 465767, 4567, 3454;')
        ]

        self.write('Query 1 results: %s<br>' % cursor1.fetchall())
        self.write('Query 2 results: %s<br>' % cursor2.fetchall())
        self.write('Query 3 results: %s' % cursor3.fetchall())

        self.finish()


class CallbackWaitHandler(BaseHandler):
    @tornado.web.asynchronous
    @gen.engine
    def get(self):

        self.db.execute('SELECT 42, 12, %s, 11;', (25,),
            callback=(yield gen.Callback('q1')))
        self.db.execute('SELECT 42, 12, %s, %s;', (23, 56),
            callback=(yield gen.Callback('q2')))
        self.db.execute('SELECT 465767, 4567, 3454;',
            callback=(yield gen.Callback('q3')))

        cursor1 = yield gen.Wait('q1')
        cursor2 = yield gen.Wait('q2')
        cursor3 = yield gen.Wait('q3')

        self.write('Query 1 results: %s<br>' % cursor1.fetchall())
        self.write('Query 2 results: %s<br>' % cursor2.fetchall())
        self.write('Query 3 results: %s' % cursor3.fetchall())

        self.finish()


def main():
    try:
        tornado.options.parse_command_line()
        application = tornado.web.Application([
            (r'/', OverviewHandler),
            (r'/query', SingleQueryHandler),
            (r'/batch', BatchQueryHandler),
            (r'/chain', QueryChainHandler),
            (r'/multi_query', MultiQueryHandler),
            (r'/callback_and_wait', CallbackWaitHandler),
        ], debug=True)

        application.db = momoko.AsyncClient({
            'host': settings.host,
            'port': settings.port,
            'database': settings.database,
            'user': settings.user,
            'password': settings.password,
            'min_conn': settings.min_conn,
            'max_conn': settings.max_conn,
            'cleanup_timeout': settings.cleanup_timeout
        })

        http_server = tornado.httpserver.HTTPServer(application)
        http_server.listen(8888)
        tornado.ioloop.IOLoop.instance().start() # the problem lies here, I believe
    except KeyboardInterrupt:
        print('Exit')


if __name__ == '__main__':
    main()

When I run the script, I get the error:

[E 121023 15:31:07 ioloop:337] Exception in I/O handler for fd 6
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 327, in start
        self._handlers[fd](fd, events)
      File "/usr/local/lib/python2.7/dist-packages/momoko/pools.py", line 313, in _io_callback
        state = self._conn.poll()
    OperationalError: asynchronous connection failed

I can connect to the webserver. If I type in “http://localhost:8888/&#8221; I see 5 links (ie “A single query”, “A batch of queries”, etc). When I click on one, however, I get:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1021, in _stack_context_handle_exception
    raise_exc_info((type, value, traceback))
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 265, in _nested
    if exit(*exc):
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 161, in __exit__
    return self.exception_handler(type, value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 114, in handle_exception
    return runner.handle_exception(typ, value, tb)
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 388, in handle_exception
    self.run()
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 343, in run
    yielded = self.gen.throw(*exc_info)
  File "/home/desktop-admin/Desktop/eclipse/workspace/jive_backend/test.py", line 35, in get
    cursor = yield gen.Task(self.db.execute, 'SELECT 42, 12, %s, 11;', (25,))
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 258, in _nested
    yield vars
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 228, in wrapped
    callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/momoko/pools.py", line 313, in _io_callback
    state = self._conn.poll()
OperationalError: asynchronous connection failed
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:17:14+00:00Added an answer on June 13, 2026 at 10:17 am

    Momoko is wrapper for psycopg2, not for mysql.

    With mysql try to use something from list on this page:

    • Twisted’s adpapi
    • txMySQL
    • Adb.py

    If you are using postgresql – try to connect to it in synchronous way first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Python and wanted to read a file, which has even
I'm very new to Python and even newer to PyQt. I've managed to create
I'm a new Python programmer who is making the leap from 2.6.4 to 3.1.1.
New to Python, have a simple, situational question: Trying to use BeautifulSoup to parse
Very new to python and can't understand why this isn't working. I have a
I am new to Python and have been studying its fundementals for 3 months
I'm a new to Python. I have installed Python 2.7 64 bit on Win7
I have new to python and django I am creating poll application and in
I'm new to Python and would like to read a config file using the
I am new to Python (I dont have any programming training either), so please

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.