python - Making async calls from an async library? -


the toy script shows application using class dependent on implementation not asyncio-aware, , doesn't work.

how fetch method of myfetcher implemented, using asyncio-aware client, while still maintaining contract _internal_validator method of fetcherapp? clear, fetcherapp , abstractfetcher cannot modified.

to use async fetch_data function inside fetch both fetch , is_fetched_data_valid functions should async too. can change them in child classes without modify parent:

import asyncio   class asyncfetcherapp(fetcherapp):     async def is_fetched_data_valid(self):  # async here         data = await self.fetcher_implementation.fetch()  # await here         return self._internal_validator(data)   class asyncmyfetcher(abstractfetcher):     def __init__(self, client):         super().__init__()         self.client = client      async def fetch(self):  # async here         result = await self.client.fetch_data()  # await here         return result   class asyncclient:     async def fetch_data(self):         await asyncio.sleep(1)  # sure works         return 1    async def main():     async_client = asyncclient()     my_fetcher = asyncmyfetcher(async_client)     fetcherapp = asyncfetcherapp(my_fetcher)     # ...     is_valid = await fetcherapp.is_fetched_data_valid()  # await here     print(repr(is_valid))   if __name__ == "__main__":     loop = asyncio.get_event_loop()     loop.run_until_complete(main()) 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -