Skip to content

Add option to turn off async on building embedded gateway #1345

@virajmehta

Description

@virajmehta

@GabrielBianconi found this pattern that seems to work:

import asyncio
from typing import Union, Awaitable, overload, Literal

import time


class AsyncTensorZeroGateway:
    def __init__(self, *, value: str):
        self.value = value

    @overload
    @staticmethod
    def build(value: str, use_async: Literal[False]) -> "AsyncTensorZeroGateway": ...

    @overload
    @staticmethod
    def build(
        value: str, use_async: Literal[True] = True
    ) -> Awaitable["AsyncTensorZeroGateway"]: ...

    @staticmethod
    def build(
        value: str, use_async: bool = True
    ) -> Union["AsyncTensorZeroGateway", Awaitable["AsyncTensorZeroGateway"]]:
        if use_async:

            async def async_wrapper():
                # Simulate some async work
                c = AsyncTensorZeroGateway(value=value)
                await asyncio.sleep(0.1)
                return c

            return async_wrapper()
        else:
            c = AsyncTensorZeroGateway(value=value)
            time.sleep(0.1)
            return c


class MyOtherClass:
    def __init__(self):
        self.c = AsyncTensorZeroGateway.build(
            value="blocking inside __init__", use_async=False
        )


async def main():
    c0 = AsyncTensorZeroGateway.build(value="blocking", use_async=False)
    c1 = await AsyncTensorZeroGateway.build(value="async", use_async=True)
    c2 = await AsyncTensorZeroGateway.build(value="async without use_async")

    print(c0.value)
    print(c1.value)
    print(c2.value)

    my_other_class = MyOtherClass()
    print(my_other_class.c.value)


asyncio.run(main())

we should add an option for building embedded and http gateway that say async_setup=False (default true)

Metadata

Metadata

Assignees

Labels

priority-highThe team is prioritizing this issue and will tackle it very soon.pythonThis issue involves Python.rustThis issue involves Rust.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions