object-store-python icon indicating copy to clipboard operation
object-store-python copied to clipboard

Allow threads

Open adriangb opened this issue 1 year ago • 1 comments

This should release the GIL and allow use in multiple threads.

I tested with this script:

from contextlib import contextmanager
import math
from time import time
from typing import Iterator

import anyio
import anyio.to_process
import anyio.to_thread
import object_store


@contextmanager
def timeit(name: str) -> Iterator[None]:
    start = time()
    yield
    print(f'{name} took {time() - start:.2f} seconds', flush=True)


def work() -> None:
    object_store.ObjectStore('gs://adriangb-public-bucket').get('yellow_tripdata_2024-01 (1).parquet')


async def awork(limiter: anyio.CapacityLimiter) -> None:
    await anyio.to_thread.run_sync(work, limiter=limiter)


async def main() -> None:
    limiter = anyio.CapacityLimiter(math.inf)

    with timeit('main'):
        async with anyio.create_task_group() as tg:
            for _ in range(32):
                tg.start_soon(awork, limiter)


if __name__ == '__main__':
    anyio.run(main)

Locally there isn't much difference, I'm IO bound. But on GCP compute that goes from ~15s to ~3s for me.

adriangb avatar Apr 30 '24 00:04 adriangb

@roeap quick ping on this

adriangb avatar May 03 '24 00:05 adriangb

@adriangb - sorry for being MIA for so long. Do you mind rebasing, and I am happy to review / merge then.

roeap avatar Jun 09 '24 13:06 roeap

@roeap will you push a 0.2.0 release after this one?

ion-elgreco avatar Jun 09 '24 18:06 ion-elgreco