twsited Asynchronous Network Framework

Source: Internet
Author: User

Twisted is an event-driven network framework that includes features such as network Protocols, threading, database management, network operations, e-mail, and more.

Twisted Introduction: http://blog.csdn.net/hanhuili/article/details/9389433

Event-driven

In short, the event driver is divided into two parts: first, registering the event; second, triggering the event.

Custom event-driven framework named: "Kill the Gentleman":

#!/usr/bin/env python#-*-coding:utf-8-*- #event_drive.pyevent_list= []  defrun (): forEventinchEvent_list:obj=event () Obj.execute ()classBasehandler (object):"""the user must inherit the class to standardize the methods of all classes (similar to the functionality of the interface)"""    defExecute (self): #必须重写此方法, error if not writtenRaiseException ('You must overwrite execute') The best event-driven framework

Programmers use the "kill the Gentleman Frame":

# !/usr/bin/env python # -*-coding:utf-8-*-  from Import event_drive   class MyHandler (event_drive. Basehandler):     def  execute (self):        print'  Event-drive Execute MyHandler'  event_drive.event_list.append (MyHandler) Event_ Drive.run ()

Protocols
Protocols describes how to handle events in a network asynchronously. HTTP, DNS, and IMAP are examples of application-layer protocols. Protocols implements the Iprotocol interface, which contains the following methods:

MakeConnection establish a connection between the transport object and the server
Called after the Connectionmade connection is established.
DataReceived call when receiving data
Connectionlost call when closing a connection


Transports
Transports represents the connection between two communication nodes in a network. Transports is responsible for describing the details of the connection, such as whether the connection is flow-oriented or datagram-oriented, flow control, and reliability. TCP, UDP, and UNIX sockets can be used as examples of transports. They are designed to "satisfy the smallest functional unit with maximum reusability" and are separated from the protocol implementation, which allows many protocols to take the same type of transmission. Transports implements the Itransports interface, which contains the following methods:

Write writes data to a physical connection sequentially, in a non-blocking fashion
Writesequence writes a list of strings to a physical connection
Loseconnection writes all pending data and then closes the connection
Getpeer obtaining address information in the peer-to-peer connection
GetHost get the address information of the local side of the connection
Separating the transports from the protocol makes it easier to test these two levels. The transmission can be simulated by simply writing a string, which is checked in this way.

twsited Asynchronous Network Framework

Related Article

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.