python
from twisted.internet import reactor
from txpostgres import txpostgres
DB_SETTINGS = {
'database': 'mydb',
'user': 'myuser',
'password': 'mypassword',
'host': 'localhost',
'port': 5432,
}
def handle_result(result):
print(result)
def execute_query(conn):
d = conn.runQuery("SELECT * FROM mytable")
d.addCallback(handle_result)
return d
def handle_error(error):
print("Error:", error)
def connect_and_query():
dbpool = txpostgres.ConnectionPool(None, **DB_SETTINGS)
d = dbpool.start()
d.addCallback(execute_query)
d.addErrback(handle_error)
d.addCallback(lambda _: dbpool.close())
d.addBoth(lambda _: reactor.stop())
reactor.callWhenRunning(connect_and_query)
reactor.run()