| 1 | # Copyright (c) 2001-2007 Twisted Matrix Laboratories. |
|---|
| 2 | # See LICENSE for details. |
|---|
| 3 | |
|---|
| 4 | """ |
|---|
| 5 | This module provides CoreFoundation event loop support for Twisted. |
|---|
| 6 | |
|---|
| 7 | In order to use this support, simply do the following:: |
|---|
| 8 | |
|---|
| 9 | | from twisted.internet import cfreactor2 |
|---|
| 10 | | cfreactor2.install() |
|---|
| 11 | |
|---|
| 12 | Then use twisted.internet APIs as usual. Stop the event loop using |
|---|
| 13 | reactor.stop(), not AppHelper.stopEventLoop(). |
|---|
| 14 | |
|---|
| 15 | IMPORTANT: tests will fail when run under this reactor. This is |
|---|
| 16 | expected and probably does not reflect on the reactor's ability to run |
|---|
| 17 | real applications. |
|---|
| 18 | |
|---|
| 19 | Maintainer: U{Phil Christensen<mailto:phil@bubblehouse.org>} |
|---|
| 20 | """ |
|---|
| 21 | |
|---|
| 22 | from twisted.internet import _threadedselect |
|---|
| 23 | from PyObjCTools import AppHelper |
|---|
| 24 | |
|---|
| 25 | class CFReactor(_threadedselect.ThreadedSelectReactor): |
|---|
| 26 | """ |
|---|
| 27 | CoreFoundation reactor. |
|---|
| 28 | |
|---|
| 29 | Cocoa drives the event loop, select() runs in a thread. |
|---|
| 30 | """ |
|---|
| 31 | _stopping = False |
|---|
| 32 | |
|---|
| 33 | def stop(self): |
|---|
| 34 | """ |
|---|
| 35 | Stop the reactor. |
|---|
| 36 | """ |
|---|
| 37 | if self._stopping: |
|---|
| 38 | return |
|---|
| 39 | self._stopping = True |
|---|
| 40 | _threadedselect.ThreadedSelectReactor.stop(self) |
|---|
| 41 | |
|---|
| 42 | def run(self, installSignalHandlers=True): |
|---|
| 43 | self.interleave(AppHelper.callAfter, installSignalHandlers=installSignalHandlers) |
|---|
| 44 | self.mainLoop() |
|---|
| 45 | |
|---|
| 46 | def mainLoop(self): |
|---|
| 47 | """ |
|---|
| 48 | Start the reactor. |
|---|
| 49 | """ |
|---|
| 50 | self.addSystemEventTrigger("after", "shutdown", AppHelper.stopEventLoop) |
|---|
| 51 | AppHelper.runEventLoop() |
|---|
| 52 | |
|---|
| 53 | def install(): |
|---|
| 54 | """ |
|---|
| 55 | Configure the twisted mainloop to be run inside the wxPython mainloop. |
|---|
| 56 | """ |
|---|
| 57 | reactor = CFReactor() |
|---|
| 58 | from twisted.internet.main import installReactor |
|---|
| 59 | installReactor(reactor) |
|---|
| 60 | return reactor |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | __all__ = ['install'] |
|---|