maykinmedia / gemma-zds-client

0 stars 0 forks source link

Add support for connection pooling/requests sessions #6

Open sergei-maertens opened 2 years ago

sergei-maertens commented 2 years ago

The desired API would be to use a client as a context manager (even in a stack of context managers) and have the connection pool re-used while the stack is not empty. When the last context manager exits, the connection pool should be closed.

Something like:

client = Client(...)

with client:
    response = client.request(...)

    with client:
        other = client.request(...)

This optimizes IO and allows passing around client instances in various call-stacks while still neatly closing unused resources.

Complication: this should be thread-safe so we can leverage concurrent.futures.ThreadPoolExecutor maximally :grimacing: