OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

how to cache asyncio coroutines

  • Thread starter Thread starter tobib
  • Start date Start date
T

tobib

Guest
I am using aiohttp to make a simple HTTP request in python 3.4 like this:

Code:
response = yield from aiohttp.get(url)

The application requests the same URL over and over again so naturally I wanted to cache it. My first attempt was something like this:

Code:
@functools.lru_cache(maxsize=128)
def cached_request(url):
    return aiohttp.get(url)

The first call to cached_request works fine, but in later calls I end up with None instead of the response object.

I am rather new to asyncio so I tried a lot of combinations of the asyncio.coroutine decorator, yield from and some other things, but none seemed to work.

So how does caching coroutines work?
<p>I am using <a href="https://aiohttp.readthedocs.org" rel="noreferrer">aiohttp</a> to make a simple HTTP request in python 3.4 like this:</p>

<pre><code>response = yield from aiohttp.get(url)
</code></pre>

<p>The application requests the same URL over and over again so naturally I wanted to cache it. My first attempt was something like this:</p>

<pre><code>@functools.lru_cache(maxsize=128)
def cached_request(url):
return aiohttp.get(url)
</code></pre>

<p>The first call to <code>cached_request</code> works fine, but in later calls I end up with <code>None</code> instead of the response object.</p>

<p>I am rather new to asyncio so I tried a lot of combinations of the <code>asyncio.coroutine</code> decorator, <code>yield from</code> and some other things, but none seemed to work.</p>

<p>So how does caching coroutines work?</p>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top