Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do web workers use actor model?

I have been trying to understand how actor model and web workers work.

In https://dzone.com/articles/html5-web-workers-classic: "Web Workers provide a message-passing model, where scripts can communicate only through well-defined immutable messages, do not share any data, and do not use synchronization mechanisms for signaling or data integrity."

To me, it sounds very similar to actor model. Is there a difference?

UPDATE: According to Benjamin Erb:

" The fundamental idea of the actor model is to use actors as concurrent primitives that can act upon receiving messages in different ways:

1.Send a finite number of messages to other actors.
2. Spawn a finite number of new actors.
3. Change its own internal behavior, taking effect when the next incoming message is handled."

The first two apply, but how about the last one?

like image 536
darjeelingtea Avatar asked Apr 15 '16 18:04

darjeelingtea


People also ask

How do web workers work?

A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page. You can continue to do whatever you want: clicking, selecting things, etc., while the web worker runs in the background.

What are web workers example?

Common examples of web workers would be: Dashboard pages that display real-time data such as stock prices, real-time active users, and so on. Fetching huge files from the server.

What are web workers?

Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface.

When would you use a web worker?

Web Workers are primarily used for CPU-intensive tasks to be run in the background without any network connectivity required to work on the tasks.


1 Answers

Yes, actor model quite describes how do the workers work. However actor model has some more abstract requirements that also depend on your implementation - but I recommend complying with the actor model. You can read the full article on Wikipedia.

There's one thing I'd like to point out to you though:

No requirement on order of message arrival

This is something that depends on your implementation and I strongly recommend to comply with this requirement. This means, for example, if sending data in chunks, give chunks indexes. Worker messages arrive in the order they're sent, but it's good not to rely on that once your code becomes complicated.

Change its own internal behavior, taking effect when the next incoming message is handled (as per your "update")

This point kinds conflicts with the previous one. But anyone who at least read some web workers tutorial, the answer is obvious: Yes, worker can do that. Consider following code:

var name = "Worker";
self.addEventListener("message", (e)=>{
  if(typeof e.data.newName=="string") {
    name = e.data.newName;
  }
  if(e.data.command == "sendName") {
    self.postMessage({myName: name});
  }
});

Needless to say, if you send new name to the worker, the response to the "sendName" messages will be different from that point. Such change to behaviour is trivial, but can be arbitrarily complex.


If you're interested in actor model also see javascript implementation Vert.x.

Note: there are ways to block between Workers, but those are hacks and are not intended. One I can think of is asynchronous XHR with server holding the lock. I don't think this counts as exception of actor model.

like image 125
Tomáš Zato - Reinstate Monica Avatar answered Oct 23 '22 04:10

Tomáš Zato - Reinstate Monica