选择Reactor和GUI综合工具包(Choosing a Reactor and GUI Toolkit Integration)

-- Jerry Marx 于 [2004-09-11 05:29:50] 最后修改本页

1. 概述(Overview)

Twisted provides a variety of implementations of the twisted.internet.reactor. The specialized implementations are suited for different purposes and are designed to integrate better with particular platforms.

Twisted提供了["http://twistedmatrix.com/documents/TwistedDocs/TwistedDocs-1.3.0/api/twisted.internet.reactor.html "twisted.internet.reactor]的很多种实现.为不同的目的和设计整合到不同平台提供各种特定的实现.

The general purpose reactor implementations are:

通常目的实现的reactor实现有:

  • The select()-based reactor
  • The poll()-based reactor

Platform-specific reactor implementations exist for:

特定平台的实现有:

  • cReactor for Unix
  • KQueue for FreeBSD
  • Win32
  • Mac OS X

The remaining custom reactor implementations provide support for integrating with the native event loops of various graphical toolkits. This lets your Twisted application use all of the usual Twisted APIs while still being a graphical application.

其它的reactor的定制实现支持和各种图形工具包的事件循环的整合.这样Twisted应用程序就可以在图形界面程序中使用通常的Twisted API了.

Twisted currently integrates with the following graphical toolkits:

Twisted现在已经整合到下面的图形工具包中:

  • GTK+ 1.2 and 2.0
  • Qt
  • Tkinter
  • WxPython

  • Win32
  • Cocoa
  • PyUI

When using applications that runnable using twistd, e.g. TAPs or plugins, there is no need to choose a reactor explicitly, since this can be chosen using twistd's -r option.

当使用可以用twisted(比方说TAPs或者plugins)应用程序,不需要显示的选择reactor,可以使用twisted的 -r 选项

In all cases, the event loop is started by calling reactor.run(). In all cases, the event loop should be stopped with reactor.stop().

所有的情况下,实现循环都是通过调用reactor.run()开始的,通过调用reactor.stop()结束.

IMPORTANT: installing a reactor should be the first thing done in the app, since any code that does from twisted.internet import reactor will automatically install the default reactor if the code hasen't already installed one.

重要提示:应用程序做的第一件事应该是安装一个reactor,如果还没有安装reactor的话执行from twisted.internet import reactor会自动的安装一个缺省的reactor.

2. Reactor功能(Reactor Functionality)

TCP

SSL

UDP

Threading

Processes

Scheduling

Platforms

select()

Y

Y

Y

Y

Y(Unix only)

Y

Unix,Win32

poll()

Y

Y

Y

Y

Y

Y

Unix

Win32

Y

Y

Y

Y

Y

Y

Win32

cfreactor

Y

Y

Y

Y

Y

Y

OS X

GTK+

Y

Y

Y

Y

Y(Unix only)

Y

Unix,Win32

Qt

Y

Y

Y

Y

Y(Unix only)

Y

Unix,Win32

kqueue

Y

Y

Y

Y

Y

Y

FreeBSD

C

Y

N

N

Y

Y

Y

Unix

3. Reactor的通用意图(General Purpose Reactors)

3.1. Select()-based Reactor

The SelectReactor is the default reactor.

SelectReactor是缺省的reactor.

   1 from twisted.internet import reactor

The SelectReactor may be explicitly installed by:

SelectReactor可以通过如下方式显示安装:

   1 from twisted.internet import default
   2 default.install()

3.2. Poll()-based Reactor

The PollReactor will work on any platform that provides poll(). With larger numbers of connected sockets, it may provide for better performance.

PollReactor可以在任何提供poll()的平台上.它可以通过大量的连接套接字而提供更好的性能.

   1 from twisted.internet import pollreactor
   2 pollreactor.install()

4. Reactor平台细节(Platform-Specific Reactors)

4.1. cReactor for Unix

The cReactor is a high-performance C implementation of the Reactor interfaces. It is currently experimental and under active development.

cReactor是一个高性能的C语言实现的Ractor接口.当前它还是试验性的,还在开发中.

   1 from twisted.internet import cReactor
   2 cReactor.install()

4.2. KQueue

The KQueue Reactor allows Twisted to use FreeBSD's kqueue mechanism for event scheduling. See instructions in the twisted.internet.kqreactor's docstring for installation notes.

KQueue Reactor允许Twisted使用FreeBSD的kqueue机制来为事件调度服务.参考twisted.internet.kqreactor的docstring以获得安装信息.

   1 from twisted.internet import kqreactor
   2 kqreactor.install()

4.3. Win32

The Win32 reactor is not yet complete and has various limitations and issues that need to be addressed. The reactor supports GUI integration with the win32gui module, so it can be used for native Win32 GUI applications.

Win32 reactor还没有完成,目前有一些限制和问题.这个reactor支持和win32gui模块的GUI整合,它可以被用于原生的win32 GUI应用程序.

   1 from twisted.internet import win32eventreactor
   2 win32eventreactor.install()

5. GUI整合Reactor(GUI Integration Reactors)

5.1. GTK+

Twisted integrates with PyGTK, versions 1.2 and 2.0. Sample applications using GTK+ and Twisted are available in the Twisted CVS.

Twisted和pyGTK的整合,版本号1.2和2.0. 使用GTK+和Twisted的样例程序可以在Twisted CVS中找到.

   1 from twisted.internet import gtkreactor
   2 gtkreactor.install()

6. GUI整合Reactor(GUI Integration Reactors

6.1. Cocoa

Twisted integrates with PyObjC, version 1.0. Sample applications using Cocoa and Twisted are available in the examples directory under Cocoa.

Twisted和PyObjC的整合,版本号1.0. 使用Cocoa和Twisted的样例程序可以在例子目录中的Cocoa目录中找到

   1 from twisted.internet import cfreactor
   2 cfreactor.install()

6.2. Qt

An example Twisted application that uses Qt can be found in doc/examples/qtdemo.py.

使用Qt的Twisted应用程序可以在doc/examples/qtdemo.py找到.

When installing the reactor, pass a QApplication instance, and if you don't a new one will be created for you.

在安装reactor的时候,传递一个QApplication实例,如果你没有这么做的话系统会自动为你创建一个.

   1 from qt import QApplication
   2 app = QApplication([])
   3 
   4 from twisted.internet import qtreactor
   5 qtreactor.install(app)

7. 没有Reactor整合的GUI(Non-Reactor GUI Integration)

7.1. Tkinter

The support for Tkinter doesn't use a specialized reactor. Instead, there is some specialized support code:

对于TKinter的支持没有使用特定的reactor,而是一些特别的支持代码

   1 from Tkinter import *
   2 from twisted.internet import tksupport
   3 
   4 root = Tk()
   5 root.withdraw()
   6 
   7 # Install the Reactor support
   8 tksupport.install(root)
   9 
  10 # at this point build Tk app as usual using the root object,
  11 # and start the program with "reactor.run()", and stop it
  12 # with "reactor.stop()".

7.2. wxPython

As with Tkinter, the support for integrating Twisted with a wxPython application uses specialized support code rather than a simple reactor.

如同Tkinter,整合Twisted和wxPython也是使用特定得支持代码而不是实现一个简单得reactor.

   1 from wxPython.wx import *
   2 from twisted.internet import wxsupport, reactor
   3 
   4 myWxAppInstance = wxApp(0)
   5 wxsupport.install(myWxAppInstance)

However, this has issues when runnin on Windows, so Twisted now comes with alternative wxPython support using a reactor. Using this method is probably better. Initialization is done in two stages. In the first, the reactor is installed:

在Windows上运行的时候目前还有一些问题,因此现在也可以使用reactor来支持wxPython.或许使用这种方法更好.初始化会分成两个阶段执行.首先,reactor被安装:

   1 from twisted.internet import wxreactor
   2 wxreactor.install()

Later, once a wxApp instance has been created, but before reactor.run() is called:

然后,wxApp进程被创建,而在reactor.run()被调用前:

   1 myWxAppInstance = wxApp(0)
   2 reactor.registerWxApp(myWxAppInstance)

An example Twisted application that uses WxWindows can be found in doc/examples/wxdemo.py

使用WxWindows的Twisted应用可以在doc/examples/wxdemo.py找到.

7.3. PyUI

As with Tkinter, the support for integrating Twisted with a PyUI application uses specialized support code rather than a simple reactor.

如同Tkinter,Twisted对于PyUI的支持也是使用特定的代码而不是一个reactor.

   1 from twisted.internet import pyuisupport, reactor
   2 
   3 pyuisupport.install(args=(640, 480), kw={'renderer': 'gl'})

An example Twisted application that uses PyUI can bve found in doc/examples/pyuidemo.py 使用PyUI的Twisted样例程序可以在doc/examples/pyuidemo.py找到.

  • 翻译 -- Jerry Marx

(目录)Index

Version: 1.3.0