扭曲的Python中的异步编程 [英] Asynchronous Programming in Python Twisted

查看:72
本文介绍了扭曲的Python中的异步编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Twisted中开发反向代理.它可以工作,但似乎过于复杂和令人费解.感觉就像是伏都教.

I'm having trouble developing a reverse proxy in Twisted. It works, but it seems overly complex and convoluted. So much of it feels like voodoo.

网络上或书籍中是否有简单,可靠的异步程序结构示例?一种最佳实践指南?当我完成程序后,我希望仍然能够以某种方式看到结构,而不是一碗意大利面.

Are there any simple, solid examples of asynchronous program structure on the web or in books? A sort of best practices guide? When I complete my program I'd like to be able to still see the structure in some way, not be looking at a bowl of spaghetti.

推荐答案

Twisted包含大量示例.特别是一个手指进化"教程,其中包含关于异步程序如何从很小的内核发展到具有许多活动部件的复杂系统的详尽解释.您可能会感兴趣的另一本教程是有关编写服务器.

Twisted contains a large number of examples. One in particular, the "evolution of Finger" tutorial, contains a thorough explanation of how an asynchronous program grows from a very small kernel up to a complex system with lots of moving parts. Another one that might be of interest to you is the tutorial about simply writing servers.

要记住有关Twisted或其他异步网络库(例如异步 MINA Deferred.如果您习惯编写直线运行的代码,并且只调用立即返回结果的函数,那么等待某些东西来回调您的想法可能会令人困惑.但是,没有什么神奇的东西,也没有关于回调的伏都教".在最低级别,反应堆只是围坐在一旁,等待着少数事情之一发生:

The key thing to keep in mind about Twisted, or even other asynchronous networking libraries (such as asyncore, MINA, or ACE), is that your code only gets invoked when something happens. The part that I've heard most often sound like "voodoo" is the management of callbacks: for example, Deferred. If you're used to writing code that runs in a straight line, and only calls functions which return immediately with results, the idea of waiting for something to call you back might be confusing. But there's nothing magical, no "voodoo" about callbacks. At the lowest level, the reactor is just sitting around and waiting for one of a small number of things to happen:

  1. 数据通过连接到达(它将在协议上调用dataReceived)
  2. 时间已过去(它将调用在callLater中注册的函数).
  3. 已接受连接(它将在注册有listenXXXconnectXXX功能的工厂上调用buildProtocol).
  4. 连接已断开(它将在相应的协议上调用connectionLost)
  1. Data arrives on a connection (it will call dataReceived on a Protocol)
  2. Time has passed (it will call a function registered with callLater).
  3. A connection has been accepted (it will call buildProtocol on a factory registered with a listenXXX or connectXXX function).
  4. A connection has been dropped (it will call connectionLost on the appropriate Protocol)

每个异步程序都首先连接这些事件中的一些,然后启动反应堆以等待它们发生.当然,发生的事件会导致更多事件被挂断或断开连接,因此您的程序将继续进行下去.除此之外,关于异步程序结构,没有什么有趣或特别之处.事件处理程序和回调只是对象,并且您的代码以通常的方式运行.

Every asynchronous program starts by hooking up a few of these events and then kicking off the reactor to wait for them to happen. Of course, events that happen lead to more events that get hooked up or disconnected, and so your program goes on its merry way. Beyond that, there's nothing special about asynchronous program structure that are interesting or special; event handlers and callbacks are just objects, and your code is run in the usual way.

这是一个简单的事件驱动引擎",向您显示此过程非常简单.

Here's a simple "event-driven engine" that shows you just how simple this process is.

# Engine
import time
class SimplestReactor(object):
    def __init__(self):
        self.events = []
        self.stopped = False

    def do(self, something):
        self.events.append(something)

    def run(self):
        while not self.stopped:
            time.sleep(0.1)
            if self.events:
                thisTurn = self.events.pop(0)
                thisTurn()

    def stop(self):
        self.stopped = True

reactor = SimplestReactor()

# Application    
def thing1():
    print 'Doing thing 1'
    reactor.do(thing2)
    reactor.do(thing3)

def thing2():
    print 'Doing thing 2'

def thing3():
    print 'Doing thing 3: and stopping'
    reactor.stop()

reactor.do(thing1)
print 'Running'
reactor.run()
print 'Done!'

在像Twisted这样的库的核心处,主循环中的功能不是sleep,而是像select()poll()这样的操作系统调用,如select,因为这是一个API,在不同平台之间差异很大,并且几乎每个GUI工具包都有其自己的版本. Twisted当前为该主题的14个不同变体提供了抽象界面.此类API提供的常见功能是提供一种说法:这是我正在等待的事件列表.入睡直到其中一个事件发生,然后醒来并告诉我是哪个事件. "

At the core of libraries like Twisted, the function in the main loop is not sleep, but an operating system call like select() or poll(), as exposed by a module like the Python select module. I say "like" select, because this is an API that varies a lot between platforms, and almost every GUI toolkit has its own version. Twisted currently provides an abstract interface to 14 different variations on this theme. The common thing that such an API provides is provide a way to say "Here are a list of events that I'm waiting for. Go to sleep until one of them happens, then wake up and tell me which one of them it was."

这篇关于扭曲的Python中的异步编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆