Twisted overview and reactor Overview

Source: Internet
Author: User
Python is a relatively new programming language, object-oriented scripting language. Many people think of Shell and Javascript when they hear the scripting language. In fact, compared with these languages, Python has some common scripting languages, but more is the new features of Python. Its strength is not clear to me. The best way to learn python is to look at the BT source code. The latest source code published by Bt is 5.2, which can be downloaded from the BitTorrent source code. The BT protocol specifications are also available in Bt specificationshttp: // www.bittorw..org/beps/bep_0003.

Okay, let's go to the point. Having said so much, does it seem to have nothing to do with the question twisted? Http://twistedmatrix.com/trac/here we twisted is an event-driven networking engine written in Python and licensed under the MIT license. Twisted is a widely recognized network programming framework in Python. If you do not learn twisted when learning Python network programming, you can only understand Python network programming, just like using Django for development websites. Both of them are famous frameworks under python. Twisted is a single-threaded event-driven network engine. There are only a few learning materials and fewer Chinese characters. Therefore, to learn twisted, you must read the English documents, that is, twisted documentationhttp: // example. In particular, many examples are provided in core documentation and example. If these examples are all run once, your twisted can be used as an example.

I mainly use the twisted factory and Protocol framework to compile a tracker server of an internal content delivery network, not based on the standard BT protocol. If you want to learn, it is best to follow the standard BT protocol. The preceding URL is also provided. As for how to use twisted, I will introduce it in detail in subsequent articles.

This article first introduces twisted's two working methods: reactor and application.
The reactor is the core of the event loop within twisted -- the loop which drives applications using twisted. the reactor provides basic interfaces to a number of services, including network communications, threading, and event dispatching.
A reactor is the core of a twisted event loop. It provides basic interfaces for services, such as network communication, thread distribution, and event distribution.

For details about reactor, see section 1 reactor overview of the low-level twisted chapter in twisted core documentationhttp: // descriptions.
The reactor I know has the following:

Reactor platform usage

Iocpreactor Win32 from twisted. Internet import iocpreactor. Reactor. Install () from twisted. Internet import Reactor

Selectreactor Win32, POSIX from twisted. Internet import Reactor

Pollreactor POSIX from twisted. Internet import pollreactor. Install () from twisted. Internet import Reactor

Epollreactor linux2.6 from twisted. Internet import epollreactor. Install () from twisted. Internet import Reactor

Kqueuereactor BSD series from twisted. Internet import kqreactor. Install () from twisted. Internet import Reactor

The above are the most frequently used reactor types, except for the kqueuereactor which I have never used. Can work normally. It is recommended that the best reactor be selected based on different platforms when programming.
Selectreactor is used by default.

The following is a small example:
Code:

From twisted. Internet. Protocol import protocol, factory
From twisted. Internet import Reactor

### Protocol implementation

# This is just about the simplest possible protocol
Class echo (Protocol ):
Def datareceived (self, data ):
"As soon as any data is stored ed, write it back ."""
Self. Transport. Write (data)

Def main ():
F = Factory ()
F. Protocol = echo
Reactor. listentcp (8000, F)
Reactor. Run ()

If _ name _ = '_ main __':
Main ()

This is the default selectreactor.

Next I will change it to epollreactor.
Code:

From twisted. Internet. Protocol import protocol, factory

### Protocol implementation

# This is just about the simplest possible protocol
Class echo (Protocol ):
Def datareceived (self, data ):
"As soon as any data is stored ed, write it back ."""
Self. Transport. Write (data)

Def main ():
F = Factory ()
F. Protocol = echo

From twisted. Internet import epollreactor
Epollreactor. Install ()

From twisted. Internet import Reactor

Reactor. listentcp (8000, F)
Reactor. Run ()

If _ name _ = '_ main __':
Main ()

In this way, the program uses epollreactor. Some people say, how do I know what reactor it uses?
You only need to add the following two lines to your program:
Import sys
Print sys. modules ['twisted. Internet. Reactor ']
With these two sentences, You can confidently tell others that you are using epoll.

The reactor will be updated at any time.
As for the application, I plan to introduce it in a specific chapter later.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/suiyunonghen/archive/2009/03/27/4029367.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.