What is a Non-Blocking framework?
Explain it like I'm 5: Imagine you want to make a deposit to your bank account. You walk in and notice there is no queue of people waiting line.
The sign over the bank teller says: "Non-blocking teller". You walk up and ask the teller to process your deposit, the teller responds: "I'm busy with another transaction, try again later".
You wait some amount of time, and try again later. You make your own decision if you want to keep trying to get your transaction processed, or not. You try and the 5th time you try, your transaction is processed immediately.
The "Blocking teller" would have told you to "stand in line as a FIFO queue". The non blocking teller says: "try again later". Which bank teller would you prefer to interact with, and why?
Definition: A non-blocking framework provides a service and returns a result immediately instead of expecting the other programs requesting a resource to wait.
In other words: When a client side program makes a call to the framework, the call will always return immediately with whatever response it has for you without expecting you to sit there waiting for something. This guarentee is nice for programmers who don't want to worry about programming around the situation where the framework expects us to wait for 5 minutes.
A concrete programming example: Suppose your framework wants to provide mutually exclusive access to a file saved on the server side. An example of a "non blocking call" would be try_lock
. If the client side wants access to the file, the framework responds: "No, try again later", rather than putting you in a queue and expecting you to sit there waiting.
The client side keeps trying for the lock, until it gets it, once it gets it, it does its business and unlocks it. The benefit of this is that whatever you try has an immediate effect.
Drawbacks to non blocking frameworks: When there is too much work to be done for a non-blocking framework, clients are denied, and fairness is not enforced, only the clients who badger the server the most get access to the service. It's not fair.
I have done a lot of searching but I am still not able to figure out where exactly should I place this Python file containing all the logic.
There are a number of options, depending on what your requirements are:
Add the logic to e.g. the Image
model. This is a useful option if you need to store per-image meta data in the database, and each model instance (each image) is processed by itself.
Add the logic as a plain Python Image
class, e.g. in a file called image.py
. Nothing in Django restricts you from adding logic other than that in the views
or models
modules. This is a good option if the image logic is a central component of your Django app (e.g. a Image processing app).
Create a separate Python project that provides the logic, then call it from your views. Make sure to install this project in your Django app's Python environment. This option is valid if the purpose of your Django app is to upload and view images, or to show the results of the image processing in direct response to a user's request, but where the image processing could be used by other projects too.
Create a separate app that processes requests asynchronously and is run separately from your Django app. This option is useful if you need to decouple the image processing from the request cycle of the app, process large number of images, or where each calculation takes too much time to solve within a request cycle's time (say within at most 500ms to 1s).
I feel it is intuitively not right since model should exclusively communicate with the back end.
There is nothing in Django that requires a model to communicate with the back end, or rather the database. I think you are mixing the semantics of what Django typically considers a model (namely, an abstraction of one or several tables in the database), v.s. the term model as a design construct (e.g. as in Domain Driven Design).
Best Answer