wx.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. """Enable wxPython to be used interacively in prompt_toolkit
  2. """
  3. from __future__ import absolute_import
  4. import sys
  5. import signal
  6. import time
  7. from timeit import default_timer as clock
  8. import wx
  9. def inputhook_wx1(context):
  10. """Run the wx event loop by processing pending events only.
  11. This approach seems to work, but its performance is not great as it
  12. relies on having PyOS_InputHook called regularly.
  13. """
  14. try:
  15. app = wx.GetApp()
  16. if app is not None:
  17. assert wx.Thread_IsMain()
  18. # Make a temporary event loop and process system events until
  19. # there are no more waiting, then allow idle events (which
  20. # will also deal with pending or posted wx events.)
  21. evtloop = wx.EventLoop()
  22. ea = wx.EventLoopActivator(evtloop)
  23. while evtloop.Pending():
  24. evtloop.Dispatch()
  25. app.ProcessIdle()
  26. del ea
  27. except KeyboardInterrupt:
  28. pass
  29. return 0
  30. class EventLoopTimer(wx.Timer):
  31. def __init__(self, func):
  32. self.func = func
  33. wx.Timer.__init__(self)
  34. def Notify(self):
  35. self.func()
  36. class EventLoopRunner(object):
  37. def Run(self, time, input_is_ready):
  38. self.input_is_ready = input_is_ready
  39. self.evtloop = wx.EventLoop()
  40. self.timer = EventLoopTimer(self.check_stdin)
  41. self.timer.Start(time)
  42. self.evtloop.Run()
  43. def check_stdin(self):
  44. if self.input_is_ready():
  45. self.timer.Stop()
  46. self.evtloop.Exit()
  47. def inputhook_wx2(context):
  48. """Run the wx event loop, polling for stdin.
  49. This version runs the wx eventloop for an undetermined amount of time,
  50. during which it periodically checks to see if anything is ready on
  51. stdin. If anything is ready on stdin, the event loop exits.
  52. The argument to elr.Run controls how often the event loop looks at stdin.
  53. This determines the responsiveness at the keyboard. A setting of 1000
  54. enables a user to type at most 1 char per second. I have found that a
  55. setting of 10 gives good keyboard response. We can shorten it further,
  56. but eventually performance would suffer from calling select/kbhit too
  57. often.
  58. """
  59. try:
  60. app = wx.GetApp()
  61. if app is not None:
  62. assert wx.Thread_IsMain()
  63. elr = EventLoopRunner()
  64. # As this time is made shorter, keyboard response improves, but idle
  65. # CPU load goes up. 10 ms seems like a good compromise.
  66. elr.Run(time=10, # CHANGE time here to control polling interval
  67. input_is_ready=context.input_is_ready)
  68. except KeyboardInterrupt:
  69. pass
  70. return 0
  71. def inputhook_wx3(context):
  72. """Run the wx event loop by processing pending events only.
  73. This is like inputhook_wx1, but it keeps processing pending events
  74. until stdin is ready. After processing all pending events, a call to
  75. time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%.
  76. This sleep time should be tuned though for best performance.
  77. """
  78. # We need to protect against a user pressing Control-C when IPython is
  79. # idle and this is running. We trap KeyboardInterrupt and pass.
  80. try:
  81. app = wx.GetApp()
  82. if app is not None:
  83. assert wx.Thread_IsMain()
  84. # The import of wx on Linux sets the handler for signal.SIGINT
  85. # to 0. This is a bug in wx or gtk. We fix by just setting it
  86. # back to the Python default.
  87. if not callable(signal.getsignal(signal.SIGINT)):
  88. signal.signal(signal.SIGINT, signal.default_int_handler)
  89. evtloop = wx.EventLoop()
  90. ea = wx.EventLoopActivator(evtloop)
  91. t = clock()
  92. while not context.input_is_ready():
  93. while evtloop.Pending():
  94. t = clock()
  95. evtloop.Dispatch()
  96. app.ProcessIdle()
  97. # We need to sleep at this point to keep the idle CPU load
  98. # low. However, if sleep to long, GUI response is poor. As
  99. # a compromise, we watch how often GUI events are being processed
  100. # and switch between a short and long sleep time. Here are some
  101. # stats useful in helping to tune this.
  102. # time CPU load
  103. # 0.001 13%
  104. # 0.005 3%
  105. # 0.01 1.5%
  106. # 0.05 0.5%
  107. used_time = clock() - t
  108. if used_time > 10.0:
  109. # print 'Sleep for 1 s' # dbg
  110. time.sleep(1.0)
  111. elif used_time > 0.1:
  112. # Few GUI events coming in, so we can sleep longer
  113. # print 'Sleep for 0.05 s' # dbg
  114. time.sleep(0.05)
  115. else:
  116. # Many GUI events coming in, so sleep only very little
  117. time.sleep(0.001)
  118. del ea
  119. except KeyboardInterrupt:
  120. pass
  121. return 0
  122. if sys.platform == 'darwin':
  123. # On OSX, evtloop.Pending() always returns True, regardless of there being
  124. # any events pending. As such we can't use implementations 1 or 3 of the
  125. # inputhook as those depend on a pending/dispatch loop.
  126. inputhook = inputhook_wx2
  127. else:
  128. # This is our default implementation
  129. inputhook = inputhook_wx3