FSX / momoko

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

How can I run the first sample of momoko #143

Closed yoosofan closed 8 years ago

yoosofan commented 8 years ago

My project needs time consuming queries. Therefore, Momoko seems the best answer.But I couldn't even run the first sample of Momoko. What is the problem of the following code:

import psycopg2
import momoko
from tornado.ioloop import IOLoop
ioloop = IOLoop.instance()

dsn= 'dbname=db1 user=postgres password=myPass host=127.0.0.1 port=5432'
conn = momoko.Connection(dsn=dsn)
future = ioloop.add_future(conn.connect(), lambda x: ioloop.stop())
ioloop.start()
print(type(future))
future.result()  # raises exception on connection error

future = conn.execute("SELECT 1")
ioloop.add_future(future, lambda x: ioloop.stop())
ioloop.start()
cursor = future.result()
rows = cursor.fetchall()
for m1 in rows:
  print(m1)

Error : Python3 on Ubuntu 16.04 with latest packages <class 'NoneType'> Traceback (most recent call last): File "test.01.py", line 11, in future.result() # raises exception on connection error AttributeError: 'NoneType' object has no attribute 'result'

haizaar commented 8 years ago

How about

future = conn.connect()
ioloop.add_future(future, lambda x: ioloop.stop())
ioloop.start()
...

On 31 May 2016 12:18, "Ahmad Yoosofan" notifications@github.com wrote:

My project needs time consuming queries. Therefore, Momoko seems the best answer.But I couldn't even run the first sample of Momoko. What is the problem of the following code:

import psycopg2 import momoko from tornado.ioloop import IOLoop ioloop = IOLoop.instance()

dsn= 'dbname=db1 user=postgres password=myPass host=127.0.0.1 port=5432' conn = momoko.Connection(dsn=dsn) future = ioloop.add_future(conn.connect(), lambda x: ioloop.stop()) ioloop.start() print(type(future)) future.result() # raises exception on connection error

future = conn.execute("SELECT 1") ioloop.add_future(future, lambda x: ioloop.stop()) ioloop.start() cursor = future.result() rows = cursor.fetchall() for m1 in rows: print(m1)

Error : Python3 on Ubuntu 16.04 with latest packages

Traceback (most recent call last): File "test.01.py", line 11, in future.result() # raises exception on connection error AttributeError: 'NoneType' object has no attribute 'result'

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/FSX/momoko/issues/143, or mute the thread https://github.com/notifications/unsubscribe/AADjWdbbg0bP3uQ34MjF84FBrXG-WRh0ks5qG_zWgaJpZM4IqVOC .

haizaar commented 8 years ago

Btw, You don't really need a future for connect operation. On 31 May 2016 12:31, "Zaar Hai" haizaar@gmail.com wrote:

How about

future = conn.connect()
ioloop.add_future(future, lambda x: ioloop.stop())
ioloop.start()
...

On 31 May 2016 12:18, "Ahmad Yoosofan" notifications@github.com wrote:

My project needs time consuming queries. Therefore, Momoko seems the best answer.But I couldn't even run the first sample of Momoko. What is the problem of the following code:

import psycopg2 import momoko from tornado.ioloop import IOLoop ioloop = IOLoop.instance()

dsn= 'dbname=db1 user=postgres password=myPass host=127.0.0.1 port=5432' conn = momoko.Connection(dsn=dsn) future = ioloop.add_future(conn.connect(), lambda x: ioloop.stop()) ioloop.start() print(type(future)) future.result() # raises exception on connection error

future = conn.execute("SELECT 1") ioloop.add_future(future, lambda x: ioloop.stop()) ioloop.start() cursor = future.result() rows = cursor.fetchall() for m1 in rows: print(m1)

Error : Python3 on Ubuntu 16.04 with latest packages

Traceback (most recent call last): File "test.01.py", line 11, in future.result() # raises exception on connection error AttributeError: 'NoneType' object has no attribute 'result'

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/FSX/momoko/issues/143, or mute the thread https://github.com/notifications/unsubscribe/AADjWdbbg0bP3uQ34MjF84FBrXG-WRh0ks5qG_zWgaJpZM4IqVOC .

yoosofan commented 8 years ago

Thank you, my problem solved. The corrected code is:

import psycopg2
import momoko
from tornado.ioloop import IOLoop
ioloop = IOLoop.instance()

dsn = "dbname=db user=postgres password=myPass host=127.0.0.1 port=5432"
conn = momoko.Pool(dsn=dsn)
future = conn.connect()
ioloop.add_future(future, lambda x: ioloop.stop())
ioloop.start()
future.result()  # raises exception on connection error

future = conn.execute("SELECT 1")
ioloop.add_future(future, lambda x: ioloop.stop())
ioloop.start()
cursor = future.result()
rows = cursor.fetchall()
for m1 in rows:
  print(m1)
yoosofan commented 8 years ago

Please change the sample of the first page on tutorial

yoosofan commented 8 years ago

Here is simpler code:

import psycopg2
import momoko
from tornado.ioloop import IOLoop
ioloop = IOLoop.instance()

dsn = "dbname=db user=postgres password=myPass host=127.0.0.1 port=5432"
conn = momoko.Pool(dsn=dsn)
ioloop.add_future(conn.connect(), lambda x: ioloop.stop())
ioloop.start()

future = conn.execute("SELECT 1")
ioloop.add_future(future, lambda x: ioloop.stop())
ioloop.start()
cursor = future.result()
rows = cursor.fetchall()
for m1 in rows:      print(m1)
haizaar commented 8 years ago

My bad - you do need to check future's result for connect operation if you want to catch connection errors.