am sorry for this dumb beginner question, but i have a real problem understanding the concept on asynchronous I/O, i dont speak about callback and other complicated stuff, i just wan to understand the beginning of “how python execute the code”
so here is the example i want to understand with
class Foo()
take a user input # line 1
seek for this input from the database # line 2
make some operation using the database output # line 3
make an output to the client and show the message to the page # line 4
so if a user will use Tornado for example, if he execute the code on the server, then, suppose we have 4 clients requesting the page where the url matches to the class Foo, then, how Python will execute the code;
ie; in python, since it’s a Script language, then every line is executing and returning the value? so does it execute the line 1 for the user 1, and then stops, and serves the line 1 for the client 2 and so on with the rest of clients, and then skip to the line 2 and so on?
Question has not many to do with python. It’s rather a question about asynchronous python frameworks (i.e tornado).
Clients in tornado are handled asynchronously. It means that when server processes clients request and hits the line where some async action is done (database query in your example – line #2) it breaks execution there and switches to another client. When database query is done notification about it goes to the queue and client waits there for its turn to be executed once more from the point he stopped (line #3).
Tornado has it’s own pattern of execution of clients. It’s called reactor pattern. Basically it means the it is looping through the queue of clients and handling them when needed (i.e request came through socket, database query finnished).
This non-blocking looping is done through operating system’s utilities like Epoll.