def test_fswatcher1(self): evtLoop = self.app.GetTraits().CreateEventLoop() activator = wx.EventLoopActivator(evtLoop) # automatically restores the old one watcher = wx.FileSystemWatcher() watcher.Add(os.getcwd()) watcher.Bind(wx.EVT_FSWATCHER, lambda evt: None)
def _ProcessEventsPhoenix(self): # this version uses the new Phoenix API # Get app app = self._GetNativeApp() # Keep reference of old eventloop instance old = wx.EventLoopBase.GetActive() # Create new eventloop and process eventLoop = app.GetTraits().CreateEventLoop() wx.EventLoopActivator(eventLoop) while eventLoop.Pending(): eventLoop.Dispatch() # Process idle eventLoop.ProcessIdle() # otherwise frames do not close # Set back the original wx.EventLoopActivator(old)
def input_handler2(): """Run the wx event loop by processing pending events only. This is like inputhook_wx1, but it keeps processing pending events until stdin is ready. After processing all pending events, a call to time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%. This sleep time should be tuned though for best performance. """ app = wx.GetApp() global POLLTIME, ON_INTERRUPT if app is not None: if not wx.Thread_IsMain(): raise Exception('wx thread is not the main thread') evtloop = wx.EventLoop() activator = wx.EventLoopActivator(evtloop) while not stdin_ready(): while evtloop.Pending(): evtloop.Dispatch() app.ProcessIdle() try: sleep(POLLTIME) except KeyboardInterrupt: if hasattr(ON_INTERRUPT, '__call__'): ON_INTERRUPT() activator = None # del activator return 0
async def MainLoop(self): evtloop = wx.GUIEventLoop() with wx.EventLoopActivator(evtloop): while not self.exiting: evtloop.DispatchTimeout(0) await asyncio.sleep(0.005) self.ProcessPendingEvents() evtloop.ProcessIdle()
async def MainLoop(self): evtloop = wx.GUIEventLoop() with wx.EventLoopActivator(evtloop): while not self.exiting: while evtloop.Pending(): evtloop.Dispatch() await asyncio.sleep(0.005) evtloop.ProcessIdle()
def update(): app = wx.GetApp() evtloop = wx.EventLoop() activator = wx.EventLoopActivator(evtloop) while evtloop.Pending(): evtloop.Dispatch() app.ProcessIdle()
def inputhook_wx3(): """Run the wx event loop by processing pending events only. This is like inputhook_wx1, but it keeps processing pending events until stdin is ready. After processing all pending events, a call to time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%. This sleep time should be tuned though for best performance. """ # We need to protect against a user pressing Control-C when IPython is # idle and this is running. We trap KeyboardInterrupt and pass. try: app = wx.GetApp() if app is not None: assert wx.Thread_IsMain() # The import of wx on Linux sets the handler for signal.SIGINT # to 0. This is a bug in wx or gtk. We fix by just setting it # back to the Python default. if not isinstance(signal.getsignal(signal.SIGINT), collections.Callable): signal.signal(signal.SIGINT, signal.default_int_handler) evtloop = wx.EventLoop() ea = wx.EventLoopActivator(evtloop) t = clock() while not stdin_ready(): while evtloop.Pending(): t = clock() evtloop.Dispatch() app.ProcessIdle() # We need to sleep at this point to keep the idle CPU load # low. However, if sleep to long, GUI response is poor. As # a compromise, we watch how often GUI events are being processed # and switch between a short and long sleep time. Here are some # stats useful in helping to tune this. # time CPU load # 0.001 13% # 0.005 3% # 0.01 1.5% # 0.05 0.5% used_time = clock() - t if used_time > 5 * 60.0: # print 'Sleep for 5 s' # dbg time.sleep(5.0) elif used_time > 10.0: # print 'Sleep for 1 s' # dbg time.sleep(1.0) elif used_time > 0.1: # Few GUI events coming in, so we can sleep longer # print 'Sleep for 0.05 s' # dbg time.sleep(0.05) else: # Many GUI events coming in, so sleep only very little time.sleep(0.001) del ea except KeyboardInterrupt: pass return 0
def myYield(self, eventsToProcess=wx.EVT_CATEGORY_ALL): """ Since the tests are usually run before MainLoop is called then we need to make our own EventLoop for Yield to actually do anything useful. """ evtLoop = self.app.GetTraits().CreateEventLoop() activator = wx.EventLoopActivator(evtLoop) # automatically restores the old one evtLoop.YieldFor(eventsToProcess)
def myYield(self, eventsToProcess=wx.EVT_CATEGORY_ALL): """ Since the tests are usually run before MainLoop is called then we need to make our own EventLoop for Yield to actually do anything useful. The method taken from wxPython tests. """ evtLoop = self._wxapp.GetTraits().CreateEventLoop() activator = wx.EventLoopActivator(evtLoop) evtLoop.YieldFor(eventsToProcess)
def __init__(self, warn_on_cancel_callback=False, loop=None): super(WxAsyncApp, self).__init__() self.loop = loop or get_event_loop() self.BoundObjects = {} self.RunningTasks = defaultdict(set) self.SetExitOnFrameDelete(True) self.exiting = asyncio.Event() self.warn_on_cancel_callback = warn_on_cancel_callback self.evtloop = wx.GUIEventLoop() self.activator = wx.EventLoopActivator(self.evtloop) self.activator.__enter__()
async def MainLoop(self): evtloop = wx.GUIEventLoop() with wx.EventLoopActivator(evtloop): while not self.exiting: if IS_MAC: # evtloop.Pending() just returns True on MacOs evtloop.DispatchTimeout(0) else: while evtloop.Pending(): evtloop.Dispatch() await asyncio.sleep(0.005) evtloop.ProcessIdle()
async def MainLoop(self): evtloop = wx.GUIEventLoop() with wx.EventLoopActivator(evtloop): while not self.exiting: if platform.system() == "Darwin": evtloop.DispatchTimeout(0) else: while self.HasPendingEvents(): self.ProcessPendingEvents() while evtloop.Pending(): evtloop.Dispatch() await asyncio.sleep(0.005) evtloop.ProcessIdle()
async def MainLoop(self): # inspired by https://github.com/wxWidgets/Phoenix/blob/master/samples/mainloop/mainloop.py evtloop = wx.GUIEventLoop() with wx.EventLoopActivator(evtloop): while not self.exiting: if IS_MAC: # evtloop.Pending() just returns True on MacOs evtloop.DispatchTimeout(0) else: while evtloop.Pending(): evtloop.Dispatch() await asyncio.sleep(0.005) self.ProcessPendingEvents() evtloop.ProcessIdle()
def _spin_wx(self): """Process all pending events in the wx event loop. This is for internal IPython use only and user code should not call this. Instead, they should issue the raw GUI calls themselves. """ import wx app = wx.GetApp() if app is not None and wx.Thread_IsMain(): evtloop = wx.EventLoop() ea = wx.EventLoopActivator(evtloop) while evtloop.Pending(): evtloop.Dispatch() app.ProcessIdle() del ea
def Run(self): # Set this loop as the active one. It will automatically reset to the # original evtloop when the context manager exits. print("Starting alternate event loop") app = wx.GetApp() with wx.EventLoopActivator(self): while True: self.DoMyStuff() # Generate and process idles events for as long as there # isn't anything else to do while not self.shouldExit and not self.Pending( ) and self.ProcessIdle(): pass if self.shouldExit: print("exiting alternate event loop") break # dispatch all the pending events and call Dispatch() to wait # for the next message if not self.ProcessEvents(): break if app.HasPendingEvents(): app.ProcessPendingEvents() # Currently on wxOSX Pending always returns true, so the # ProcessIdle above is not ever called. Call it here instead. if 'wxOSX' in wx.PlatformInfo: self.ProcessIdle() # Proces remaining queued messages, if any while True: checkAgain = False if wx.GetApp() and wx.GetApp().HasPendingEvents(): wx.GetApp().ProcessPendingEvents() checkAgain = True if 'wxOSX' not in wx.PlatformInfo and self.Pending(): self.Dispatch() checkAgain = True if not checkAgain: break return self.exitCode
def inputhook_wx(): """Run the wx event loop by processing pending events only. This keeps processing pending events until stdin is ready. After processing all pending events, a call to time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%. This sleep time should be tuned though for best performance. """ # We need to protect against a user pressing Control-C when IPython is # idle and this is running. We trap KeyboardInterrupt and pass. try: app = wx.GetApp() if app is not None: assert is_wxMain() if not callable(signal.getsignal(signal.SIGINT)): signal.signal(signal.SIGINT, signal.default_int_handler) evtloop = wx_EvtLoop() ea = wx.EventLoopActivator(evtloop) t = clock() while not stdin_ready() and not update_requested(): while evtloop.Pending(): t = clock() evtloop.Dispatch() if callable(getattr(app, 'ProcessIdle', None)): app.ProcessIdle() if callable(getattr(evtloop, 'ProcessIdle', None)): evtloop.ProcessIdle() # We need to sleep at this point to keep the idle CPU load # low. However, if sleep to long, GUI response is poor. used_time = clock() - t ptime = 0.001 if used_time > 0.10: ptime = 0.05 if used_time > 3.00: ptime = 0.25 if used_time > 30.00: ptime = 1.00 sleep(ptime) del ea clear_update_request() except KeyboardInterrupt: if callable(ON_INTERRUPT): ON_INTERRUPT() return 0
def ping(timeout=0.001): "ping wx" try: t0 = clock() app = wx.GetApp() if app is not None: assert is_wxMain() # Make a temporary event loop and process system events until # there are no more waiting, then allow idle events (which # will also deal with pending or posted wx events.) evtloop = wx_EventLoop() ea = wx.EventLoopActivator(evtloop) t0 = clock() while clock() - t0 < timeout: evtloop.Dispatch() app.ProcessIdle() del ea except: pass
def inputhook_wx1(context): """Run the wx event loop by processing pending events only. This approach seems to work, but its performance is not great as it relies on having PyOS_InputHook called regularly. """ app = wx.GetApp() if app is not None: assert wx.Thread_IsMain() # Make a temporary event loop and process system events until # there are no more waiting, then allow idle events (which # will also deal with pending or posted wx events.) evtloop = wx.EventLoop() ea = wx.EventLoopActivator(evtloop) while evtloop.Pending(): evtloop.Dispatch() app.ProcessIdle() del ea return 0
async def MainLoop(self): """Asynchronous version of combined asyncio and wxPython event loops.""" # inspired by https://github.com/wxWidgets/Phoenix/blob/master/samples/mainloop/mainloop.py evtloop = wx.GUIEventLoop() with wx.EventLoopActivator(evtloop): while any(self.top_windows) and not self.exiting: if IS_MAC: # evtloop.Pending() just returns True on MacOs evtloop.DispatchTimeout(0) else: while evtloop.Pending(): evtloop.Dispatch() # We don't stop more than necessary, doing otherwise will create latency await asyncio.sleep(0.0000000000001) self.ProcessPendingEvents() evtloop.ProcessIdle() # At this point we just exit the main loop self.ExitMainLoop()
async def MainLoop(self): print("MainLoop") # inspired by https://github.com/wxWidgets/Phoenix/blob/master/samples/mainloop/mainloop.py evtloop = wx.GUIEventLoop() with wx.EventLoopActivator(evtloop): while not self.exiting: if IS_MAC: # evtloop.Pending() just returns True on MacOs evtloop.DispatchTimeout(0) print("x", end='') else: while evtloop.Pending(): evtloop.Dispatch() print("y", end='') await asyncio.sleep(0.005) print("z", end='') # self.DeletePendingEvents() # experiment self.ProcessPendingEvents() evtloop.ProcessIdle() print(random.randint(0, 100), end=' ') sys.stdout.flush()
def alive(): return True class Server(Thread): def run(self): server = SimpleXMLRPCServer(("localhost", 8000), allow_none=True) server.register_introspection_functions() server.register_function(plot) server.register_function(alive) print "Listening on port 8000..." server.serve_forever() server = Server() server.start() app = wx.GetApp() assert app is not None assert wx.Thread_IsMain() evtloop = wx.EventLoop() ea = wx.EventLoopActivator(evtloop) while 1: gui_lock.acquire() while evtloop.Pending(): evtloop.Dispatch() app.ProcessIdle() gui_lock.release() gui_lock.acquire() gui_lock.release() sleep(0.001)