CPU usage for queries, psycopg 2 vs 3

Поиск
Список
Период
Сортировка
От David Raymond
Тема CPU usage for queries, psycopg 2 vs 3
Дата
Msg-id DB7PR07MB3916A230837909499A87EF3287E8A@DB7PR07MB3916.eurprd07.prod.outlook.com
обсуждение исходный текст
Ответы Re: CPU usage for queries, psycopg 2 vs 3
Re: CPU usage for queries, psycopg 2 vs 3
Re: CPU usage for queries, psycopg 2 vs 3
Список psycopg
I've noticed that CPU usage for queries seems to have drastically increased going from psycopg2 to psycopg3.

In psycopg2 a connection would sit quietly while waiting for the results, using next to no CPU.
In psycopg it looks like the threads are using their core at full power, continuously checking for the result as fast
aspossible, and never sleeping or relaxing.
 

See below for a simple example script. During the psycopg2 part while the 8 threads are waiting on results my processor
usagesits at the normal idling 2-3%. But in the psycopg3 part, while the 8 threads are waiting on results my processor
usagejumps up to 62-63% or so.
 

Is there a setting or configuration I'm missing?
This is on Windows, where I install with "pip install psycopg2-binary" and "pip install psycopg[binary]"
Current versions shown by pip list:
psycopg            3.1.10    (This gets installed even when telling it to install psycopg[binary])
psycopg-binary     3.1.10
psycopg2-binary    2.9.7

And their reported .__version__
psycopg.__version__: 3.1.10
psycopg2.__version__: 2.9.7 (dt dec pq3 ext lo64)

I've got several scripts where I open up multiple connections to multiple servers and run a number of queries on them,
where98% of my machine's time should be just relaxing and waiting for the server to finish the next 1 to 15 minute long
query.I don't need to continue the very microsecond the query completes. Is there anything I can do to bring CPU usage
backdown with psycopg (3) ?
 

Thank you,



import sys
import threading

import psycopg
import psycopg2

def connectAndRunSleep3():
    conn = psycopg.connect(host = "localhost", dbname = "...", user = "...", password = "...", autocommit = True)
    with conn.cursor() as cur:
        cur.execute("select pg_sleep(15);")
    conn.close()

def connectAndRunSleep2():
    conn = psycopg2.connect(host = "localhost", dbname = "...", user = "...", password = "...")
    conn.autocommit = True
    with conn.cursor() as cur:
        cur.execute("select pg_sleep(15);")
    conn.close()

if __name__ == "__main__":
    print("Python version:", sys.version)
    print("psycopg.__version__:", psycopg.__version__)
    print("psycopg2.__version__:", psycopg2.__version__)
    
    numThreads = 8
    
    print("Check your CPU usage during this run")
    
    print(f"Querying with {numThreads:,d} threads in psycopg2...")
    threads = []
    for n in range(numThreads):
        thread = threading.Thread(target = connectAndRunSleep2)
        threads.append(thread)
        thread.start()
    for thread in threads:
        thread.join()
    print("All joined.")
    
    print(f"Querying with {numThreads:,d} threads in psycopg3...")
    threads = []
    for n in range(numThreads):
        thread = threading.Thread(target = connectAndRunSleep3)
        threads.append(thread)
        thread.start()
    for thread in threads:
        thread.join()
    print("All joined.")

В списке psycopg по дате отправления:

Предыдущее
От: Daniele Varrazzo
Дата:
Сообщение: Re: conn.read_only not honored in autocommit mode
Следующее
От: Adrian Klaver
Дата:
Сообщение: Re: CPU usage for queries, psycopg 2 vs 3