Ticket #1833: cfreactor2.py

File cfreactor2.py, 1.7 KB (added by rikyu, 7 years ago)

CFReactor implementation using TSR

Line 
1# Copyright (c) 2001-2007 Twisted Matrix Laboratories.
2# See LICENSE for details.
3
4"""
5This module provides CoreFoundation event loop support for Twisted.
6
7In order to use this support, simply do the following::
8
9    |  from twisted.internet import cfreactor2
10    |  cfreactor2.install()
11
12Then use twisted.internet APIs as usual. Stop the event loop using
13reactor.stop(), not AppHelper.stopEventLoop().
14
15IMPORTANT: tests will fail when run under this reactor. This is
16expected and probably does not reflect on the reactor's ability to run
17real applications.
18
19Maintainer: U{Phil Christensen<mailto:phil@bubblehouse.org>}
20"""
21
22from twisted.internet import _threadedselect
23from PyObjCTools import AppHelper
24
25class 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
53def 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']