Skip to content
Myr

Rewrote becoming big and foreword

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 9e1111c · Browse files · 2 files+72 added−56 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+72 added−56 removed

  • other:+72 added−56 removed

2 files+72 added−56 removed

modified:docs/becomingbig.rst+60 added-40 removed
1 unmodified line
23Becoming Big4============56Your application is becoming more and more complex? Flask is really not7designed for large scale applications and does not attempt to do so, but8that does not mean you picked the wrong tool in the first place.6Your application is becoming more and more complex? If you suddenly7realize that Flask does things in a way that does not work out for your8application there are ways to deal with that.910Flask is powered by Werkzeug and Jinja2, two libraries that are in use at11a number of large websites out there and all Flask does is bring those12two together. Being a microframework, Flask is literally a single file.13What that means for large applications is that it's probably a good idea14to take the code from Flask and put it into a new module within the15applications and expand on that.12two together. Being a microframework Flask does not do much more than13combinding existing libraries - there is not a lot of code involved.14What that means for large applications is that it's very easy to take the15code from Flask and put it into a new module within the applications and16expand on that.1717What Could Be Improved?18-----------------------18Flask is designed to be extended and modified in a couple of different19ways:2020For instance it makes a lot of sense to change the way endpoints (the21names of the functions / URL rules) are handled to also take the module22name into account. Right now the function name is the URL name, but23imagine you have a large application consisting of multiple components.24In that case, it makes a lot of sense to use dotted names for the URL25endpoints.21- Subclassing. The majority of functionality can be changed by creating22 a new subclass of the :class:`~flask.Flask` class and overriding23 some methods.2427Here are some suggestions for how Flask can be modified to better 28accommodate large-scale applications:25- Flask extensions. For a lot of reusable functionality you can create26 extensions.2730- get rid of the decorator function registering which causes a lot31 of troubles for applications that have circular dependencies. It32 also requires that the whole application is imported when the system33 initializes or certain URLs will not be available right away. A34 better solution would be to have one module with all URLs in there and35 specifying the target functions explicitly or by name and importing36 them when needed.37- switch to explicit request object passing. This requires more typing38 (because you now have something to pass around) but it makes it a39 whole lot easier to debug hairy situations and to test the code.40- integrate the `Babel`_ i18n package or `SQLAlchemy`_ directly into the41 core framework.28- Forking. If nothing else works out you can just take the Flask29 codebase at a given point and copy/paste it into your application30 and change it. Flask is designed with that in mind and makes this31 incredible easy. You just have to take the package and copy it32 into your application's code and rename it (for example to33 `framework`). Then you can start modifying the code in there.3443.. _Babel: http://babel.edgewall.org/44.. _SQLAlchemy: http://www.sqlalchemy.org/35Why consider Forking?36---------------------3746Why does Flask not do all that by Default?47------------------------------------------38The majority of code of Flask is within Werkzeug and Jinja2. These39libraries do the majority of the work. Flask is just the paste that glues40those together. For every project there is the point where the underlying41framework gets in the way (due to assumptions the original developers42had). This is natural because if this would not be the case, the43framework would be a very complex system to begin with which causes a44steep learning curve and a lot of user frustration.4549There is a huge difference between a small application that only has to50handle a couple of requests per second and with an overall code complexity51of less than 4000 lines of code and something of larger scale. At some52point it becomes important to integrate external systems, different53storage backends and more.46This is not unique to Flask. Many people use patched and modified47versions of their framework to counter shortcomings. This idea is also48reflected in the license of Flask. You don't have to contribute any49changes back if you decide to modify the framework.5055If Flask was designed with all these contingencies in mind, it would be a56much more complex framework and harder to get started with.51The downside of forking is of course that Flask extensions will most52likely break because the new framework has a different import name and53because of that forking should be the last resort.5455Scaling like a Pro56------------------5758For many web applications the complexity of the code is less an issue than59the scaling for the number of users or data entries expected. Flask by60itself is only limited in terms of scaling by your application code, the61data store you want to use and the Python implementation and webserver you62are running on.6364Scaling well means for example that if you double the amount of servers65you get about twice the performance. Scaling bad means that if you add a66new server the application won't perform any better or would not even67support a second server.6869There is only one limiting factor regarding scaling in Flask which are70the context local proxies. They depend on context which in Flask is71defined as being either a thread or a greenlet. Separate processes are72fine as well. If your server uses some kind of concurrency that is not73based on threads or greenlets, Flask will no longer be able to support74these global proxies. However the majority of servers are using either75threads, greenlets or separate processes to achieve concurrency which are76all methods well supported by the underlying Werkzeug library.
modified:docs/foreword.rst+12 added-16 removed
15 unmodified lines
1617For example, Flask uses thread-local objects internally so that you don't18have to pass objects around from function to function within a request in19order to stay threadsafe. While this is a really easy approach and saves20you a lot of time, it also does not scale well to large applications.21It's especially painful for more complex unittests, and when you suddenly22have to deal with code being executed outside of the context of a request,23such as in cron jobs.20you a lot of time, it might also cause some troubles for very large21applications because changes on these thread-local objects can happen22anywhere in the same thread.2325Flask provides some tools to deal with the downsides of this approach, but26the core problem remains. Flask is also based on convention over27configuration, which means that many things are preconfigured and will28work well for smaller applications but not so well for larger ones. For29example, by convention, templates and static files are in subdirectories30within the Python source tree of the application.24Flask provides some tools to deal with the downsides of this approach but25it might be an issue for larger applications. Flask is also based on26convention over configuration, which means that many things are27preconfigured and will work well for smaller applications but not so well28for larger ones. For example, by convention, templates and static files29are in subdirectories within the Python source tree of the application.3032But don't worry if your application suddenly grows larger33and you're afraid Flask might not grow with it. Even with34larger frameworks, you'll eventually discover that you need35something the framework just cannot do for you without modification.36If you are ever in that situation, check out the :ref:`becomingbig`37chapter.31However Flask is not much code and built in a very solid foundation and32with that very easy to adapt for large applications. If you are33interested in that, check out the :ref:`becomingbig` chapter.3435A Framework and an Example36--------------------------37
50 unmodified lines