def install_twisted(interval=1, start_running=True): try: from twisted.internet import _threadedselect _threadedselect.install() def waker(func): sublime.set_timeout(func, interval) from twisted.internet import reactor reactor.interleave(waker) except ImportError: return False return True
def __install(): log = logging.getLogger('tpython') log.info('setting up twisted reactor in ipython loop') from twisted.internet import _threadedselect _threadedselect.install() from twisted.internet import reactor from collections import deque from IPython.lib import inputhook from IPython import InteractiveShell q = deque() def reactor_wake(twisted_loop_next, q=q): q.append(twisted_loop_next) def reactor_work(*_args): if q: while len(q): q.popleft()() return 0 def reactor_start(*_args): log.info('starting twisted reactor in ipython') reactor.interleave(reactor_wake) # @UndefinedVariable inputhook.set_inputhook(reactor_work) def reactor_stop(): if reactor.threadpool: # @UndefinedVariable log.info('stopping twisted threads') reactor.threadpool.stop() # @UndefinedVariable log.info('shutting down twisted reactor') reactor._mainLoopShutdown() # @UndefinedVariable ip = InteractiveShell.instance() ask_exit = ip.ask_exit def ipython_exit(): reactor_stop() return ask_exit() ip.ask_exit = ipython_exit reactor_start() return reactor
def main_darwin(): """ Custom main for OSX. """ from PyObjCTools import AppHelper from AppKit import NSNotificationCenter, NSApplication from apaf.utils.osx_support import ApafAppWrapper, TorFinishedLoadNotification from twisted.internet import _threadedselect try: _threadedselect.install() except Exception as e: log.err(e) app = NSApplication.sharedApplication() delegate = ApafAppWrapper.alloc().init() delegate.setMainFunction_andReactor_(main, reactor) app.setDelegate_(delegate) AppHelper.runEventLoop()
#!/usr/bin/env python from twisted.internet import _threadedselect _threadedselect.install() from twisted.python import log import sys log.startLogging(sys.stdout) from coherence.base import Coherence, ControlPoint import tingbot import tingbot_gui as gui from layout import NOTEBOOK_BUTTON_SIZE,MAIN_PANEL import library import playlist import current import time tingbot.screen.fill("black") current_button = gui.ToggleButton((10,0),NOTEBOOK_BUTTON_SIZE,align="topleft",label="Current") current_panel = current.CurrentPanel() current_panel.visible = False playlist_button = gui.ToggleButton((160,0),NOTEBOOK_BUTTON_SIZE,align="top",label="Playlist") playlist_panel = playlist.PlaylistPanel(current_panel) lib_button = gui.ToggleButton((310,0),NOTEBOOK_BUTTON_SIZE,align="topright",label="Library") lib_panel = library.LibraryPanel(playlist_panel) nb = gui.NoteBook([(lib_button,lib_panel),(playlist_button,playlist_panel),(current_button,current_panel)])
def install_twisted_reactor(**kwargs): '''Installs a threaded twisted reactor, which will schedule one reactor iteration before the next frame only when twisted needs to do some work. Any arguments or keyword arguments passed to this function will be passed on the the threadedselect reactors interleave function. These are the arguments one would usually pass to twisted's reactor.startRunning. Unlike the default twisted reactor, the installed reactor will not handle any signals unless you set the 'installSignalHandlers' keyword argument to 1 explicitly. This is done to allow kivy to handle the signals as usual unless you specifically want the twisted reactor to handle the signals (e.g. SIGINT). .. note:: Twisted is not included in iOS build by default. To use it on iOS, put the twisted distribution (and zope.interface dependency) in your application directory. ''' import twisted # prevent installing more than once if hasattr(twisted, '_kivy_twisted_reactor_installed'): return twisted._kivy_twisted_reactor_installed = True # don't let twisted handle signals, unless specifically requested kwargs.setdefault('installSignalHandlers', 0) # install threaded-select reactor, to use with own event loop from twisted.internet import _threadedselect _threadedselect.install() # now we can import twisted reactor as usual from twisted.internet import reactor from twisted.internet.error import ReactorNotRunning from collections import deque from kivy.base import EventLoop from kivy.logger import Logger from kivy.clock import Clock # will hold callbacks to twisted callbacks q = deque() # twisted will call the wake function when it needs to do work def reactor_wake(twisted_loop_next): '''Wakeup the twisted reactor to start processing the task queue ''' Logger.trace("Support: twisted wakeup call to schedule task") q.append(twisted_loop_next) # called every frame, to process the reactors work in main thread def reactor_work(*args): '''Process the twisted reactor task queue ''' Logger.trace("Support: processing twisted task queue") while len(q): q.popleft()() global _twisted_reactor_work _twisted_reactor_work = reactor_work # start the reactor, by telling twisted how to wake, and process def reactor_start(*args): '''Start the twisted reactor main loop ''' Logger.info("Support: Starting twisted reactor") reactor.interleave(reactor_wake, **kwargs) Clock.schedule_interval(reactor_work, 0) # make sure twisted reactor is shutdown if eventloop exists def reactor_stop(*args): '''Shutdown the twisted reactor main loop ''' if reactor.threadpool: Logger.info("Support: Stooping twisted threads") reactor.threadpool.stop() Logger.info("Support: Shutting down twisted reactor") reactor._mainLoopShutdown() try: reactor.stop() except ReactorNotRunning: pass import sys sys.modules.pop('twisted.internet.reactor', None) global _twisted_reactor_stopper _twisted_reactor_stopper = reactor_stop # start and stop the reactor along with kivy EventLoop Clock.schedule_once(reactor_start, 0) EventLoop.bind(on_stop=reactor_stop)
# Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. from twisted.internet import _threadedselect _threadedselect.install() from itertools import count from twisted.internet import reactor from twisted.internet.defer import Deferred from twisted.python.failure import Failure from twisted.python.runtime import seconds try: # Python 3 from queue import Empty, Queue except ImportError: # Python 2 from Queue import Empty, Queue class TwistedManager: def __init__(self): self.twistedQueue = Queue() self.key = count() self.results = {} def getKey(self): # get a unique identifier return next(self.key)
from PyObjCTools import AppHelper from twisted.internet._threadedselect import install reactor = install() # import classes required to start application import WSTApplicationDelegateClass import WSTConnectionWindowControllerClass # pass control to the AppKit AppHelper.runEventLoop()
# Name: VenueEventClient.py # Purpose: A secure group messaging service client that handles Access # Grid venue events. # Created: 2006/01/10 # RCS-ID: $Id: VenueEventClient.py,v 1.10 2007-05-04 20:26:44 eolson Exp $ # Copyright: (c) 2006 # Licence: See COPYING.TXT #----------------------------------------------------------------------------- import sys try: from twisted.internet import _threadedselect as threadedselectreactor except: from twisted.internet import threadedselectreactor try: threadedselectreactor.install() from twisted.internet import reactor except: pass from AccessGrid.InsecureVenueEventClient import mainWithUI, GenerateRandomString from AccessGrid.InsecureVenueEventClient import BaseVenueEventClient, TestMessages from AccessGrid import SecureGroupMsgClient from AccessGrid.XMLGroupMsgClient import XMLGroupMsgClient class SecureVenueEventClient(BaseVenueEventClient): ''' A version of the EventClient that supports encryption. ''' def __init__(self, location,
def install_twisted_reactor(**kwargs): '''Installs a threaded twisted reactor, which will schedule one reactor iteration before the next frame only when twisted needs to do some work. any arguments or keyword arguments passed to this function will be passed on the the threadedselect reactors interleave function, these are the arguments one would usually pass to twisted's reactor.startRunning Unlike the default twisted reactor, the installed reactor will not handle any signals unnless you set the 'installSignalHandlers' keyword argument to 1 explicitly. This is done to allow kivy to handle teh signals as usual, unless you specifically want the twisted reactor to handle the signals (e.g. SIGINT).''' import twisted # prevent installing more than once if hasattr(twisted, '_kivy_twisted_reactor_installed'): return twisted._kivy_twisted_reactor_installed = True # dont let twisted handle signals, unless specifically requested kwargs.setdefault('installSignalHandlers', 0) # install threaded-select reactor, to use with own event loop from twisted.internet import _threadedselect _threadedselect.install() # now we can import twisted reactor as usual from twisted.internet import reactor from collections import deque from kivy.base import EventLoop from kivy.logger import Logger from kivy.clock import Clock # will hold callbacks to twisted callbacks q = deque() # twisted will call the wake function when it needsto do work def reactor_wake(twisted_loop_next): Logger.trace("Support: twisted wakeup call to schedule task") q.append(twisted_loop_next) # called every frame, to process the reactors work in main thread def reactor_work(*args): Logger.trace("Support: processing twisted task queue") while len(q): q.popleft()() # start the reactor, by telling twisted how to wake, and process def reactor_start(*args): Logger.info("Support: Starting twisted reactor") reactor.interleave(reactor_wake, **kwargs) Clock.schedule_interval(reactor_work, 0) # make sure twisted reactor is shutdown if eventloop exists def reactor_stop(*args): '''will shutdown the twisted reactor main loop ''' if reactor.threadpool: Logger.info("Support: Stooping twisted threads") reactor.threadpool.stop() Logger.info("Support: Shutting down twisted reactor") reactor._mainLoopShutdown() # start and stop teh reactor along with kivy EventLoop EventLoop.bind(on_start=reactor_start) EventLoop.bind(on_stop=reactor_stop)
# # main.py # IPython1Sandbox # # Created by Barry Wark on 3/4/08. # Copyright __MyCompanyName__ 2008. All rights reserved. # #import modules required by application import objc import Foundation import AppKit from PyObjCTools import AppHelper from twisted.internet import _threadedselect reactor = _threadedselect.install() # import modules containing classes required to start application and load MainMenu.nib import IPython1SandboxAppDelegate import IPython.frontend.cocoa.cocoa_frontend # pass control to AppKit AppHelper.runEventLoop()
# conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. from twisted.internet._threadedselect import install reactor = install() from PyObjCTools import AppHelper import Automator import BWToolkitFramework import Foundation import AppKit import MGScopeBar import objc # import modules containing classes required to start application and # load MainMenu.nib: from friendly import app, master, account, contacts # pass control to AppKit
__docformat__ = "restructuredtext en" # The standard imports import os import sys import signal import time import threading from optparse import Option # Our imports try: from twisted.internet import _threadedselect as threadedselectreactor except: from twisted.internet import threadedselectreactor threadedselectreactor.install() import twisted from twisted.internet import reactor from AccessGrid.Toolkit import Service, MissingDependencyError from AccessGrid.VenueServer import VenueServer from AccessGrid import Log from AccessGrid.Platform.Config import SystemConfig from AccessGrid.hosting import SecureServer, InsecureServer from AccessGrid import Toolkit from AccessGrid.Security import CertificateManager from M2Crypto import threading as M2Crypto_threading # Global defaults log = None venueServer = None
enough. written by Ben Olsen """ import pygame from pygame.locals import * from ocempgui.widgets import * from ocempgui.widgets.Constants import * from ocempgui.widgets.components import TextListItem from ocempgui.object import BaseObject from ocempgui.events import INotifyable from twisted.internet._threadedselect import install install() from twisted.internet import reactor, protocol # local signals SIG_REC = 'rec' SIG_SEND = 'send' COMMAND = 'command' class LoginScreen (BaseObject): """The Login Screen contains Entries for server, port, and user name. The button emits a signal containing this info, which is picked up by the factory and used in reactor.connectTCP. It inherits from BaseObject to make signal processing easier. """ def __init__ (self, re): BaseObject.__init__ (self)