Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103992
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | "Martin A. Brown" <martin@linux-ip.net> |
| Newsgroups | comp.lang.python |
| Subject | Re: Caching function results |
| Date | Thu, 3 Mar 2016 12:59:23 -0800 |
| Lines | 43 |
| Message-ID | <mailman.164.1457038775.20602.python-list@python.org> (permalink) |
| References | <7a134b64-b7a7-46c6-9f89-cf166450f972@lists.xtsubasa.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=US-ASCII |
| X-Trace | news.uni-berlin.de ySZpevmuPqWJwXCPtuVp1AH5zoT4X7dDOrfZEoGaogyw== |
| Return-Path | <martin@linux-ip.net> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'yet.': 0.03; 'cache': 0.05; '[0]': 0.07; 'cc:addr:python-list': 0.09; 'cached': 0.09; 'python.': 0.11; 'value.': 0.15; 'clear.': 0.16; 'count.': 0.16; 'from:addr:martin': 0.16; 'luck,': 0.16; 'received:hsd1.or.comcast.net': 0.16; 'received:io': 0.16; 'received:or.comcast.net': 0.16; 'received:psf.io': 0.16; 'sfxlen:0': 0.16; 'memory': 0.17; '(in': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'martin': 0.22; 'arguments': 0.22; 'function,': 0.22; 'produces': 0.22; 'rid': 0.22; 'cc:no real name:2**0': 0.22; "python's": 0.23; 'implemented': 0.24; 'header :In-Reply-To:1': 0.24; 'figure': 0.27; 'module.': 0.27; 'received:24': 0.28; 'values': 0.28; 'calculated': 0.29; "i'd": 0.31; 'entry': 0.31; 'included': 0.32; 'expensive': 0.32; 'maybe': 0.33; 'problem': 0.33; 'url:python': 0.33; 'received:comcast.net': 0.33; 'mapping': 0.35; 'tasks': 0.35; 'url:org': 0.36; 'data.': 0.36; 'url:library': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'charset:us-ascii': 0.37; 'doing': 0.38; 'thank': 0.38; 'means': 0.39; 'called': 0.40; 'some': 0.40; 'url:3': 0.60; 'received:network': 0.61; 'sample': 0.63; 'great': 0.63; 'limit': 0.65; 'greetings': 0.71; 'subject:results': 0.91 |
| X-X-Sender | mabrown@macron.wonderfrog.net |
| In-Reply-To | <7a134b64-b7a7-46c6-9f89-cf166450f972@lists.xtsubasa.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:103992 |
Show key headers only | View raw
Greetings Pavel, > Suppose, I have some resource-intensive tasks implemented as > functions in Python. Those are called repeatedly in my program. > It's guranteed that a call with the same arguments always produces > the same return value. I want to cache the arguments and return > values and in case of repititive call immediately return the > result without doing expensive calculations. Great problem description. Thank you for being so clear. [I snipped sample code...] This is generically called memoization. > Do you like this design or maybe there's a better way with > Python's included batteries? In Python, there's an implementation available for you in the functools module. It's called lru_cache. LRU means 'Least Recently Used'. > I'd also like to limit the size of the cache (in MB) and get rid > of old cached data. Don't know how yet. You can also limit the size of the lru_cache provided by the functools module. For this function, the size is calculated by number of entries--so you will need to figure out memory size to cache entry count. Maybe others who have used functools.lru_cache can help you with how they solved the problem of mapping entry count to memory usage. Good luck, -Martin [0] https://docs.python.org/3/library/functools.html#functools.lru_cache -- Martin A. Brown http://linux-ip.net/
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Caching function results "Martin A. Brown" <martin@linux-ip.net> - 2016-03-03 12:59 -0800
csiph-web