comparison docs/rendered/_sources/pymonkey.txt @ 116:06269ca0b36c

Made the operation callback doctest easier to read.
author Atul Varma <varmaa@toolness.com>
date Mon, 17 Aug 2009 03:52:51 -0700
parents f4c550369332
children d30576edf797
comparison
equal deleted inserted replaced
115:f4c550369332 116:06269ca0b36c
216 This function is one of the few thread-safe functions available 216 This function is one of the few thread-safe functions available
217 to a JS runtime, and together with 217 to a JS runtime, and together with
218 :meth:`set_operation_callback()` can be used to abort the 218 :meth:`set_operation_callback()` can be used to abort the
219 execution of long-running code. 219 execution of long-running code.
220 220
221 For instance: 221 For instance, we first create an operation callback to stop
222 long-running code by throwing an exception:
222 223
223 >>> import time, threading 224 >>> import time, threading
224 >>> cx = pymonkey.Runtime().new_context() 225 >>> cx = pymonkey.Runtime().new_context()
225 >>> def stop_running_code(cx): 226 >>> def stop_running_code(cx):
226 ... raise Exception('JS took too long to execute.') 227 ... raise Exception('JS took too long to execute.')
227 >>> cx.set_operation_callback(stop_running_code) 228 >>> cx.set_operation_callback(stop_running_code)
229
230 Then we create a watchdog thread to trigger the operation
231 callback once a long amount of time has passed:
232
228 >>> def watchdog_thread(): 233 >>> def watchdog_thread():
229 ... time.sleep(0.1) 234 ... time.sleep(0.1) # An eternity to a computer!
230 ... cx.trigger_operation_callback() 235 ... cx.trigger_operation_callback()
231 >>> thread = threading.Thread(target=watchdog_thread) 236 >>> thread = threading.Thread(target=watchdog_thread)
232 >>> thread.start() 237 >>> thread.start()
238
239 Now, when we execute code that takes too long to run, it gets
240 aborted:
241
233 >>> try: 242 >>> try:
234 ... cx.evaluate_script(cx.new_object(), 'while (1) {}', 243 ... cx.evaluate_script(cx.new_object(), 'while (1) {}',
235 ... '<string>', 1) 244 ... '<string>', 1)
236 ... except pymonkey.error, e: 245 ... except pymonkey.error, e:
237 ... print cx.get_object_private(e.args[0]) 246 ... print cx.get_object_private(e.args[0])