python - Flask - Store values in memory between requests -


i have single page application - angularjs on front , flask on backend lets user upload file (xlsx, csv...) , interactively analyze/query file

essentially user loads file memory on first upload , subsequent ajax calls tap file in memory. im not sure how keep file in memory between subsequent requests (ajax).

the g variable erased after each request , if understand right used access values across request (set before_request , available through views

the request context local request. did manage set value on current_app , able access in subsequent ajax calls

# on first file upload, load file memory , set variable on current_app:  flask import current_app @app.route('/upload', methods =['post']) def upload():    ...    upload file memory    ...    current_app.file = file_in_memory    @app.route('/subsequent_call') def subsequent():     # i'm able access file in memory through      current_app.file set earlier      return current_app.file.number_of_lines() 

this method of storing file in memory on current_app doesnt seem right , feels dirty/hackish. scale @ ?

i pickle file after every request , pull on each request. storing/pickling , refetching file every time memory when user interactively querying data seems heavy/inefficient

is there other elegant/right way this, app_context, werkzeug locals etc ? or thinking wrong ?

storing file in way not going work if webserver spawning multiple processes (workers) handle requests, , how production servers works.

further keep file object in memory not going scale if server load increases, can either save file in file system , initialize pandas object during every requests. can compare loading pickled object , see faster. have consider overhead of pickling not unpickling.

edit: explanation of why wont work in production

gunicorn , similar webservers spawn multiple workers unless restricting in config, worker separate process , each process has own python execution environment. lets first request hits worker1 , create variable current_app.file = file_in_memory in process. second request hit worker2 has own python execution environment variable not available because not shared across processes. in fact there might value in variable belongs different user request.

so in all

  1. it not guarantee same object available across requests
  2. it overridden user simultaneously using app

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -