Hands-On Enterprise Application Development with Python
上QQ阅读APP看书,第一时间看更新

Concurrency in enterprise applications

Enterprise applications are large and usually deal with a lot of user-initiated actions such as data retrieval, updates, and so on. Now, let's take a short example scenario for our BugZot application, where a user may submit a graphic attachment along with their bug report. This is actually quite a common process when filing a bug that may affect the application UI or that displays an error on the UI. Now, every user may submit an image, which may differ in quality and hence their sizes may vary. This may involve images that are very small in size and  images that may have very large sizes and high resolutions. As an application developer, you may know that storing an image with 100% quality can, at times, not only waste the storage quota of the application but may also cause the rendering of the bug report to be slow, if, every time, the full resoultion image needs to be downloaded by the client from the server.

One of the possible workarounds to this situation is to process images once they are uploaded and store them in a resolution that we determine to be a good trade-off between quality and performance. Now, image processing is a CPU-intensive task and can take quite some time, depending on the resolution of the uploaded image. If we process this in a sequential manner, we may end up with a small problem. Until the image has been converted to a specific resolution, the user-created bug report will not be submitted to the bug database. This is something that will cause a long wait time during heavy loads, while also causing users an increased level of frustration.

Now, to solve this issue, we can use concurrency. Once we have the image attached to the bug uploaded and validated as an image, we may offload the post-processing of the image to a separate background thread or a processing queue and can successfully submit bug to the application database. This is possible because the bug report capturing is a task that does not depend on the image post-processing task, and hence both can be executed in parallel. This allows for improved response times and also a greater set of optimizations as our bug reporting application becomes more and more complex.

This is just one of the example scenarios but is not the only one that may benefit from the use of concurrency in application programming. There are many more such scenarios, such as the deletion of a user account, which may warrant the removal of database records for that user, or sending account activation emails when a new user registers.

Now, let's see how we can implement the concept of concurrent programming with the use of Python as the language of choice for our application development.