-1

I wrote a Python Flask App for different image processing tasks. It seems redundant to post the same image over and over again, if I want to perform multiple image processing tasks on the same image. So I was wondering if I can include some kind of cache in my app that stores the last 10 images that have been posted.

import .....

app = Flask(__name__)

@app.route('/processing/task1', methods=["POST"])
def task1():
...
return

@app.route('/processing/task2', methods=["POST"])
def task2():
...
return

@app.route('somethingcompletelydifferent', methods=["POST"])
def different():
...
return

if __name__ == '__main__':
app.run(debug=config.app['debug'], port=config.app['port'], host=config.app['host'])

My aim would be that when I am running

answer = requests.post("http://localhost:5000/processing/task1", files=arg).content

the image contained in arg is only being transmitted, if it hasn't (yet/for a while) been transmitted. Is there a way to do that in the app? I am really confused right now and can't figure out how to do it - probably because I am missing some terms and basic knowledge in that field and fail to google it effectively ... Thanks!

(I am using python 3.7)

petezurich
  • 9,280
  • 9
  • 43
  • 57
K1K1
  • 19
  • 5
  • Take a look on this post: https://stackoverflow.com/questions/626057/is-it-possible-to-cache-post-methods-in-http – Danilo Muñoz Nov 05 '18 at 11:45
  • Okay, I've read the post a couple of times. I guess it does answer my question? But again I am more and more confused and unable to apply it to my specific problem ... ^^" So concerning my problem: I should not cache? I can't cache the image that's being send? I can't cache the results of the task#() functions? I can cache, but the cache is being emptied as soon as there is a new POST request ... which would make it unuseful in my case? – K1K1 Nov 05 '18 at 12:31
  • Hi, I suggest reading more about HTTP stack. @lijo-jose suggests a cache over functions and it does not save you from transferring images over network. If the code on client side is yours his idea could work. But it seems you want to implement it server side and work for any kind of client. In this scenario how your backend could know if image was already processed or not without receiving the image by itself? One idea would be hashing image and creating another service to check if that hash was already sent before calling the another method that process the entire image. – Danilo Muñoz Nov 06 '18 at 13:19

1 Answers1

0

You can use lrucache for that

from functools import lru_cache

@lru_cache(maxsize=32)
def process():
   # ur code 
Lijo Jose
  • 317
  • 1
  • 3
  • 13