Asynchronous access
In the preceding examples, the cursor.execute() method will block the program for the time a query is executing in the database. When the query is long, the program can be blocked for a long time, which sometimes is not a desired behavior. Psycopg 2 provides a way to execute queries asynchronously. Basically, it means that the Python program can do something else while a long query is being executed by the database server.
To use this feature, the database connection should be created as an asynchronous one. This is done by passing the async=1 argument to the connect() function. The process of connection itself is also asynchronous. Now the Python program should be made to wait until the connection is established. This ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access