NET: https://twistedmatrix.com/trac/
Http://www.cnblogs.com/wy-wangyan/p/5252271.html
What is Twisted?
Twisted is a Event-driven networking engine written in Python and licensed under the open source ? MIT license. Twisted runs on Python 2 and a ever growing subset also works with Python 3.
Twisted is an event-driven asynchronous network communication architecture implemented by Python. Can be on the official online browsing twisted source code, also has svn can download, the source code is still relatively clear and understandable. The following code or example, many of the documents are from the official website.
The first question is why the project is called twisted? My understanding is that due to the main component of the Twisted deferred list, there are two callback chains, each of which contains one (Callback,errback) pair. Twisted follow the callback chain from the first callback execution, if the error executes the next callback function corresponding to the errback, if the execution succeeds, then go back to the next callback to continue execution. Isn't it twisted?
The second question is what is an event-driven asynchronous network framework?
We have written about the network communication program are familiar with the socket, all know select/poll/epoll these network multiplexing programs, are corresponding to read or write operations, the composition of twisted contains a reacotor, is a lot of asynchronous event framework has the concept of reactor mode , this is a singleton mode, with only one reactor running at a time. Reactor.run (), we're talking about an event loop based on an event, twisted waits for the event to be ready, and then calls the callback function to return the result. Of course it is based on the Select/poll/epoll input/output (read, write) event.
About reactor mode: Please go here, this Daniel's translator: http://www.cnblogs.com/pugang/p/4621373.html
So what is async? Similarly, we know that there are several patterns of network communication, and in various interviews, there are also questions about synchronous, asynchronous, blocking, non-blocking differences.
There are many articles on this explanation, and the authoritative reference is described in UNIX network Programming for UNIX network I/O patterns. Say my own understanding, in Linux all is a file, socket of course, network I/O is a kind of I/O. Our application also contains two parts of the content, one is the kernel call of the user state is a kernel state. Network I/O contains two parts of the content:
1. The application is trapped in the kernel state waiting for data ready
2. Copy the data from the kernel space to the user space.
Blocking is the wait in both phases, all blocked, waiting for the kernel to return.
Non-blocking is the familiar socket.setblocking (0), or non-blocking flag set by FNCTL; it just blocks in the second phase, and at one stage, the application knows that the data is not ready, and immediately returns the error code Eagain/ewouldblock, Know that the data is ready to end and return after copying.
Synchronization refers to the completion of waiting for I/O operations.
Asynchronous refers to the need to wait for the I/O operation to complete, return directly, wait until the second phase completes, the kernel actively notifies the process to end.
So you say the difference, I think is blocking/non-blocking corresponds to a current state, synchronous/asynchronous focus on the overall situation or operation.
Today we're going to say event-driven, which says we rely on Select/poll/epoll, they are network multiplexed I/O, and they don't need to build multiple threads, and the thread pool can monitor multiple FD at the same time. Some call them asynchronous (synchronous) non-blocking i/0, but in practice they are also synchronous blocking, Select/poll/epoll blocking in select (), epoll.wait () These functions, wait until the data is ready, then start the first phase, then the second stage, The second stage is I/O blocking, so they are blocked in two places and need to wait for I/O operations to complete. That there is no asynchronous I/O, asynchronous I/O requires operating system support, there seems to be no satisfactory asynchronous I/O, and some implementations are based on the threading/thread pool simulation implementation.
That twisted, since the use of select/poll/epoll, why it is called an event-based asynchronous framework. Because it mode asynchronous I/O, it uses the reactor mode to handle the event loop, using deferred to handle the callback function, the user can write a callback function, and then put in the deferred list, twsited will call these callback functions after the event notification is ready, execution is complete, Returns the result. See, is not asynchronous I/O. Other event-based asynchronous frameworks, like Java's Netty, are also emulated.
Written here today, the next prepare to analyze Select/epoll These multiplexed I/O, and then start with the Socket/epoll example to begin the twisted explanation. If there is a mistake, please correct me and learn together.
Twisted is an event-driven asynchronous network communication architecture implemented by Python.