|
3 | 3 | import sys |
4 | 4 | import platform |
5 | 5 |
|
| 6 | +from ._notebook_helpers import _isnotebook |
| 7 | + |
| 8 | +if _isnotebook: |
| 9 | + import IPython |
| 10 | + |
6 | 11 | # Unresolved bug: rate(X) yields only about 0.8X iterations per second. |
7 | 12 |
|
8 | 13 | MIN_RENDERS = 10 |
@@ -210,3 +215,32 @@ def __call__(self, maxRate=100): |
210 | 215 | self.rateCount += 1 |
211 | 216 |
|
212 | 217 | self.lastEndRate = _clock() |
| 218 | + |
| 219 | + |
| 220 | +class _RateKeeper2(RateKeeper): |
| 221 | + |
| 222 | + def __init__(self, interactPeriod=INTERACT_PERIOD, interactFunc=simulateDelay): |
| 223 | + self.rval = 30 |
| 224 | + self.tocall = None |
| 225 | + super(_RateKeeper2, self).__init__(interactPeriod=interactPeriod, interactFunc=self.sendtofrontend) |
| 226 | + |
| 227 | + def sendtofrontend(self): |
| 228 | + # This is called by the rate() function, through rate_control _RateKeeper callInteract(). |
| 229 | + # See the function commsend() for details of how the browser is updated. |
| 230 | + |
| 231 | + # Check if events to process from front end |
| 232 | + if _isnotebook: |
| 233 | + if IPython.__version__ >= '3.0.0' : |
| 234 | + kernel = get_ipython().kernel |
| 235 | + parent = kernel._parent_header |
| 236 | + ident = kernel._parent_ident |
| 237 | + kernel.do_one_iteration() |
| 238 | + kernel.set_parent(ident, parent) |
| 239 | + |
| 240 | + def __call__(self, N): # rate(N) calls this function |
| 241 | + self.rval = N |
| 242 | + if self.rval < 1: raise ValueError("rate value must be greater than or equal to 1") |
| 243 | + super(_RateKeeper2, self).__call__(self.rval) ## calls __call__ in rate_control.py |
| 244 | + |
| 245 | +# The rate function: |
| 246 | +rate = _RateKeeper2(interactFunc = simulateDelay(delayAvg = 0.001)) |
0 commit comments