Guest User

Untitled

a guest
Jun 24th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. import os
  2. from contextlib import contextmanager
  3. from psycopg2.pool import ThreadedConnectionPool
  4.  
  5. DSN = "host=127.0.0.1 dbname={db} user={user} password={password}"
  6.  
  7. PG_DB = os.environ.get('PG_DB')
  8. PG_USER = os.environ.get('PG_USER')
  9. PG_PASSWORD = os.environ.get('PG_PASSWORD')
  10.  
  11. pg_pool = ThreadedConnectionPool(1, 5, DSN.format(db=PG_DB, user=PG_USER, password=PG_PASSWORD))
  12.  
  13.  
  14. @contextmanager
  15. def get_cursor(commit=True):
  16. with pg_pool.getconn() as conn:
  17. try:
  18. with conn.cursor() as cur:
  19. yield cur
  20. finally:
  21. if commit:
  22. conn.commit()
  23. pg_pool.putconn(conn)
Add Comment
Please, Sign In to add comment