2

I am currently building a web application built on Flask Framework with around 10 user accounts in the future when the application has been finished.

There is a Class with heavy module (Compute-intensive), built and used in this application, served as one of the frequently used key features, and I have run into some issues and am seeking for some solutions (let's named it as Class A in file a.py)

Originally, I imported the Class A directly into one of the view file, and created a route function for it, that once an user clicks the button which invokes this route, the route function will then create an instance of Class A, and this instance runs based on received data (like Json). But I found the system can be slow down as the instance of Class A has to be created every single time when the user uses the feature frequently, (also there can be 10 users), and Class A is too heavy to be created again and again.

Therefore I am thinking is there anyway that I can create the instance of Class A for only one time (e.g., the time that the Flask application starts), and each logged in user can access this instance rather than create it over and over again?

Thanks in advance

nonemaw
  • 619
  • 1
  • 7
  • 16

2 Answers2

2

Flask Requests are stateless, so to preserve data for a user across requests the options are limited. Here are some ideas:

  1. Serialize the class instance, store it in a flask session (just a wrapper for browser session cookies), retrieve later.

  2. Store it in a database, retrieve later when needed

  3. Pickle it, dump it using user name, retrieve when needed.

Alternatively, depending on the application, a cache solution might good enough (ig Flask-caching). The route/view would instantiate the class the first time it's called and return a value.

If the view is called again with the same arguments/data, the previous return value is returned without running the view function again.

gtalarico
  • 4,409
  • 1
  • 20
  • 42
  • Thanks for the response! But my intention is not to preserve the data, but to create some "global instance" for allowing users to invoke it globally. Do you think this would be a good way if I created some sort of `meta` class, using class attribute as a cache, e.g. `Meta.a_instance = A()`, and then import this class across `view` files? (like creating a globally-accessible naming space under class `Meta`) – nonemaw Jan 23 '18 at 05:40
  • See extensions for flask, they are exactly for this purpose. – Christian Sauer Jan 23 '18 at 05:44
  • You can instantiate the class module level of your view file and refer to it within your view functions – gtalarico Jan 23 '18 at 06:26
0

Flask has extensions, which can be setup at startup, exactly like you need. The docs are here: http://flask.pocoo.org/docs/0.12/extensiondev/

You can probably ignore the whole first part about diskutils etc. and jump to "Initializing Extensions".

We used this exact extension point for this purpose and it works at it should.

You could also use a singleton pattern in your class, but the extension point works well with the rest of the flask ecosystem.

Christian Sauer
  • 10,351
  • 10
  • 53
  • 85