def test_browser_worker(): """Main script.""" from browser import bind, document, worker result = document.select_one('.result') inputs = document.select("input") # Create a web worker, identified by a script id in this page. myWorker = worker.Worker("worker") @bind(inputs, "change") def change(evt): """Called when the value in one of the input fields changes.""" # Send a message (here a list of values) to the worker myWorker.send([x.value for x in inputs]) @bind(myWorker, "message") def onmessage(e): """Handles the messages sent by the worker.""" result.text = e.data # Code of the worker script: """Web Worker script.""" # In web workers, "window" is replaced by "self". from browser import bind, self @bind(self, "message") def message(evt): """Handle a message sent by the main script. evt.data is the message body. """ try: result = int(evt.data[0]) * int(evt.data[1]) workerResult = f'Result: {result}' # Send a message to the main script. # In the main script, it will be handled by the function bound to # the event "message" for the worker. self.send(workerResult) except ValueError: self.send('Please write two numbers') ## exercise event handlers for f in change, onmessage, message: f(evt)
#from browser import bind, self import sys import time import traceback import javascript import random import json import copy from browser import bind, self, window from pyangelo_consts import * from AngeloTurtle import * PyAngeloWorker = worker.Worker("executor") my_turtle = AngeloTurtle() my_turtle.visible = False keys_buff = None array = None input_buff = None inputs = None class PyAngeloImage(): def __init__(self, image): self.img = image self.height = image.naturalHeight
import tb as traceback from browser import bind, console, document, window, html, DOMNode, worker, timer from browser.widgets.dialog import Dialog, InfoDialog if not hasattr(window, "SharedArrayBuffer"): InfoDialog("Not available", "This program cannot run because SharedArrayBuffer is not supported.<br>" "This is probably because your server does not set the required<br>" "HTTP headers:<br>" "cross-origin-embedder-policy= 'require-corp'<br>" "cross-origin-opener-policy= 'same-origin'") sys.exit() py_worker = worker.Worker("python-worker") class Trace: def __init__(self): self.buf = "" def write(self, data): self.buf += str(data) def format(self): """Remove calls to function in this script from the traceback.""" lines = self.buf.split("\n") stripped = [lines[0]] for i in range(1, len(lines), 2): if __file__ in lines[i]:
def start(self, src): global myWorker myWorker.terminate() myWorker = worker.Worker("executor") myWorker.bind("message", onmessage) myWorker.send(["run", src])
KEY_UP = 38 KEY_RIGHT = 39 KEY_DOWN = 40 KEY_PAGEUP = 0xff55 KEY_PAGEDOWN = 0xff56 KEY_END = 0xff57 KEY_BEGIN = 0xff58 # Create a web worker, identified by a script id in this page. sab_proto = window.SharedArrayBuffer sab = sab_proto.new(1) test_data = window.Array.new(sab) test_data[0] = 42 myWorker = worker.Worker("executor") myWorker.send(sab) def onmessage(e): #alert("Message received from executor:" + str(e.data)); if e.data[0].lower() == "reveal": # successful graphics_main.commands = e.data[1] graphics_main.execute_commands() # now wait 1 frame before continuing the code execution in the worker thread timer.set_timeout(graphics_main.next_frame, 16)
from browser import document, html, bind, worker, alert from browser.timer import set_interval, clear_interval, set_timeout from algo import STATES_COLOR from random import random, randrange DIM_X = 40 DIM_Y = 30 start_cell = None end_cell = None # Construct the worker and send the first message algo_worker = worker.Worker("worker") algo_worker.send({"init": (DIM_X, DIM_Y)}) algo = "bfs" running = False idx = 0 queue_exe = list() timer = None ### Initialize HTML grid table ### for i in range(DIM_Y): row = html.TR() row_list = [] for j in range(DIM_X): cell = html.TD('', id=f"cell_{i}_{j}",