An Introduction to Asynchronous Programming and Twisted
2010-10-09 00:59
423 查看
An Introduction to Asynchronous Programming and Twisted
Aug 7th, 2009 | Filed under Blather, Programming
, Python
, Software
Leave a comment
| Trackback
Part 1: In Which We Begin at the Beginning
Preface
Someone recently postedto the Twisted
mailing list
asking for something like the “Twisted introduction for people on a
deadline”. Full disclosure: this isn’t it. On the spectrum of
introductions to Twisted and asynchronous programming in Python, it may
be on the exact opposite end. So if you don’t have any time, or any
patience, this isn’t the introduction you are looking for.
However, I also believe that if you are new to asynchronous
programming, a quick introduction is simply not possible, at least if
you are not a genius. I’ve used Twisted successfully for a number of
years and having thought about how I initially learned it (slowly), and
what I found difficult, I’ve come to the conclusion that much of the
challenge does not stem from Twisted per se, but rather in the
acquisition of the “mental model” required to write and understand
asynchronous code. Most of the Twisted source code is clear and well
written, and the online documentation is good, at least by the standards
of most free software. But without that mental model, reading the
Twisted codebase, or code that uses Twisted, or even much of the
documentation, will result in confusion and headache.
So the first parts of this introduction are designed to help you
acquire that model and only later on will we introduce the features of
Twisted. In fact, we will start without using Twisted at all, instead
using simple Python programs to illustrate how an asynchronous system
works. And once we get into Twisted, we will begin with very low-level
aspects that you would not normally use in day-to-day programming.
Twisted is a highly abstracted system and this gives you tremendous
leverage when you use it to solve problems. But when you are learning
Twisted, and particularly when you are trying to understand how Twisted
actually works, the many levels of abstraction can cause troubles. So we
will go from the inside-out, starting with the basics.
And once you have the mental model in place, I think you will find reading the Twisted documentation
, or just browsing the source code
, to be much easier. So let’s begin.
The Models
We will start by reviewing two (hopefully) familiar models in orderto contrast them with the asynchronous model. By way of illustration we
will imagine a program that consists of three conceptually distinct
tasks which must be performed to complete the program. We will make
these tasks more concrete later on, but for now we won’t say anything
about them except the program must perform them. Note I am using “task”
in the non-technical sense of “something that needs to be done”.
The first model we will look at is the single-threaded synchronous model, in Figure 1 below:
Figure 1: the synchronous model
This is the simplest style of programming. Each task is perfomed one
at a time, with one finishing completely before another is started. And
if the tasks are always performed in a definite order, the
implementation of a later task can assume that all earlier tasks have
finished without errors, with all their output available for use — a
definite simplification in logic.
异步程序twisted介绍
第一章 最开始的开始
最近总有人做了什么后在twisted邮件组里去说“twisted 怎么去弄个期限";大部分的结论是:不能这样;
相关文章推荐
- An Introduction to Asynchronous Programming and Twisted (3)
- An Introduction to Asynchronous Programming and Twisted (1)
- An Introduction to Asynchronous Programming and Twisted (2)
- An Introduction to Asynchronous Programming and Twisted (笔记)
- How to Design Programs: An Introduction to Programming and Computing
- An Introduction to Programming and Numerical Methods in MATLAB
- An Introduction to Interactive Programming in Python (Part 2) week 6 (Classes and Tied images)
- An introduction to class loading and debugging tools
- An Introduction to Language Processing with Perl and Prolog: An Outline of Theories, Implementation,
- Understanding Windows CardSpace: An Introduction to the Concepts and Challenges of Digital Identitie
- CUDA系列学习(一)An Introduction to GPU and CUDA
- Quiz 6a Question 7————An Introduction to Interactive Programming in Python
- 《麻省理工开放课程:计算机科学及编程导论》(MIT ocw 6.00 Introduction to Computer Science and Programming)[中英字幕][oCourse]更新至第15讲[PSP适用][MP4]
- Introduction to Programming Using Visual Basic 2005, An (6th Edition)
- CUDA系列学习(一)An Introduction to GPU and CUDA
- 【coursera 学习笔记】An Introduction to Interactive Programming in Python--week0
- Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —"Pong"
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 1 Rock-paper-scissors
- Mini-project # 4 - "Pong"___An Introduction to Interactive Programming in Python"RICE"
- Quiz 6b Question 8————An Introduction to Interactive Programming in Python