Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
feat: support async generators
Browse files Browse the repository at this point in the history
  • Loading branch information
Vixtir committed Oct 24, 2023
1 parent e5460c6 commit fb0e71d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
29 changes: 27 additions & 2 deletions odd_collector_sdk/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@ def _split(
data_source_oddrn=self._adapter.get_data_source_oddrn(), items=items
)

class AsyncGeneratorJob(AbstractJob):
async def start(self):
with log_execution(self._adapter.config.name):
async for del_ in self._get_data_entity_list():
await self.send_metadata(metadata=del_)

async def _split(
self, data_entity_lists: Union[DataEntityList, Iterable[DataEntityList]]
) -> Generator[DataEntityList, Any, Any]:
async for data_entity_list in data_entity_lists:
for index, items in enumerate(
chunks(self._chunk_size, data_entity_list.items), start=1
):
logger.debug(
f"[{self._adapter.config.name}] Yield batch #{index} with {len(items)} items"
)
yield DataEntityList(
data_source_oddrn=self._adapter.get_data_source_oddrn(), items=items
)

async def _get_data_entity_list(self) -> Generator[DataEntityList, Any, Any]:
data_entity_lists = self._adapter.get_data_entity_list()
async for data_entity_list in self._split(data_entity_lists):
yield data_entity_list

class AsyncJob(AbstractJob):
async def start(self):
Expand Down Expand Up @@ -89,9 +113,10 @@ def _get_data_entity_list(self) -> Generator[DataEntityList, Any, Any]:

def create_job(api: PlatformApi, adapter: Adapter, chunk_size: int) -> AbstractJob:
if isasyncgenfunction(adapter.get_data_entity_list):
raise ValueError("Async generator is not supported.")
logger.debug(f"Is an async generator {adapter.config.name=}")
return AsyncGeneratorJob(api, adapter, chunk_size)
if iscoroutinefunction(adapter.get_data_entity_list):
logger.debug(f"Is async {adapter.config.name=}")
logger.debug(f"Is an async {adapter.config.name=}")
return AsyncJob(api, adapter, chunk_size)
else:
return SyncJob(api, adapter, chunk_size)
18 changes: 18 additions & 0 deletions odd_collector_sdk/utils/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from funcy import decorator
from odd_models.models import DataEntity, DataEntityList

from ..logger import logger


@decorator
def log_data_entity(call):
data_entity: DataEntity = call()
logger.debug(data_entity.json(exclude_none=True))
return data_entity

@decorator
def write_data_entity_list(call, fname: str):
data_entities: DataEntityList = call()
with open(fname, "w+") as f:
f.write(data_entities.json(exclude_none=True))
return data_entities

0 comments on commit fb0e71d

Please sign in to comment.