2

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?

E.Z.
  • 6,393
  • 11
  • 42
  • 69
Abdelouahab Pp
  • 4,252
  • 11
  • 42
  • 65

2 Answers2

2

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.

yakxxx
  • 2,841
  • 2
  • 21
  • 22
  • so what if the line 2 is using a synchronous library (Pymongo for exemple), then the program must go till the line 4 to serve the second client? – Abdelouahab Pp Oct 08 '12 at 11:00
  • 1
    yes, indeed. Using sync library will block execution of all clients effectively hanging your entire server since DB query is done. Use txmongo instead or for example delegate pymongo requests to RESTfull API which you will call then from Tornado server with async http client. When writing async server policy is to return from handler ASAP to let another clients be handled. – yakxxx Oct 08 '12 at 11:09
  • because what i thought in the past that only line 2 is the problem of blocking the database, so when it finishs, it will continue to line 4 and so on, so the bottlneck in only in line 2 – Abdelouahab Pp Oct 08 '12 at 11:31
1

I think your question has more to do with the way python handles multithreading.

Although the interpreter will never use more than one core of your processor (some infos here and here), it supports multithreading in the sense different threads can be executed while others are waiting for I/O. In your example above, client2 (line #1) could be served while client1 is waiting for the database call (line #2) to return.

Regarding "since it's a Script language, then every line is executing and returning the value?", no, I cannot think of anything like that; You can use generators to do something like "return values for every line", but I guess it is not what you are looking for.

Community
  • 1
  • 1
E.Z.
  • 6,393
  • 11
  • 42
  • 69
  • about generators, tornado uses them, http://www.tornadoweb.org/documentation/gen.html because in the first time, i thought that because it's not like in C or C++ where you wait to compile time to execute, i said that a user can be served only using some lines that it needs and let him wait a time to serve another – Abdelouahab Pp Oct 08 '12 at 11:37