Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Clone this wiki locally
Define WebWorker Code
Any code blocked under
with webworker: is split off and injected into the webworker when it is spawned. Note that the webworker also copies all functions and classes from the main thread's namespace into the webworker, this way you do not need worry about splitting apart your code and using importScripts inside the webworker.
with webworker: def mywebworkerfunction(a): ... class Something(): def send(self, ob): ...
Create a new Worker
myworker = spawn( Something() )
Send Data to Worker
sends object to webworker. inside the webworker this calls the
send method of the spawned class.
myworker <- object
Waits until the next message comes from myworker.
result = <- myworker
Calling Worker Functions
result = <- mywebworkerfunction(x)
Calling Worker Methods
result = <- myworker.somemethod(x)
Continues if worker currently has no new messages.
select: case result = <- myworker1: print result case result = <- myworker2: print result
Multiple Return Data
Spawned objects can return a data stream to the main thread using
self <- object.
send method is called the worker is written to from the main thread using
worker <- object.
Note: if sending objects from the main thread to the worker, you must restore the
__proto__ property of the object sent, if you will call methods on it in the webworker.
with webworker: class Worker: def send(self, input): self <- 'hello' self <- 'world' return 'foobar'
Sending and Receiving Class Instances
When sending or getting data from the worker, data is actually cloned by the browser (using structured cloning), this strips away the
__proto__ attribute of the object and causes it to lose its class prototype, preventing you from calling methods on the sent object.
__proto__ and class of a sent object can automatically be restored, when you statically type the input arguments and return type of the function, the transpiler and runtime will automatically restore the class type, so that you can call methods on the object.
- restoring the
__proto__attribute only happens on the toplevel object, so any sub-objects it may contain will not have
- when static types are used outside of the webworker, they are instead used for runtime type checking, and not for restoring the
- webworkers are not builtin to NodeJS, so they do not work server side.
class SharedClass: def foobar(self, x,y,z): return x+y+z with webworker: def somefunction() -> SharedClass: s = SharedClass(10,20,30) return s class MyWorker: def send(self, obj:SharedClass ) -> SharedClass: print obj.foobar(1,2,3) return obj