Do web workers use actor model?
Asked Answered
K

1

7

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?

Kitti answered 15/4, 2016 at 18:27 Comment(5)
I'm not exactly sure how is this question and answers to it useful to you. Are you asking just to ask? How are possible differences relevant?Progressionist
My interest is theoretical, so far. I am trying to form a mental map about the concepts of concurrency. I have tried to search if web workers are actors, and I find it really strange that they seem to be very similar but actor model is never mentioned when web workers are discussed.Kitti
I suspect that's because nobody cares. For practical purposes it doesn't matter and for academic purposes there's no need to mention web workers in particular. Also, they were sure not purposely engineered to be actors. I am giving you -1 because your update to the question clearly shows you didn't even try to research sufficiently how workers work. I do also vote that this question is closed as I don't see what answer you exactly expect and what should it focus on. I suggest you try traditional forums or [computer science.se] for building mental maps. On SO, we focus on specific problems.Progressionist
Sorry, I don't understand how does my update show I didn't try to research how workers work? I searched for several hours before asking, so that's not the case. Could you point out if there is something fundamentally wrong with my reasoning? As for asking from the wrong place, should I remove this question?Kitti
Do not remove the question, unless you want to. Everything stated above is my opinion, other people might not agree. What I found wrong is that if you tried to implement simple web-worker, you'd found out what you ask most likely. That's what I call research. Googling is not research imho.Progressionist
D
6

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.

Diplo answered 15/4, 2016 at 21:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.