These forums are currently read-only due to receiving more spam than actual discussion. Sorry.

It is currently Sat Dec 02, 2017 4:19 pm Advanced search

Workers without separate script

Here you can discuss things related to HTML and the Web in general that do not fit in to other categories.

Workers without separate script

Postby cheezy » Mon Dec 14, 2009 10:24 am

Why do web workers always have to be defined in a separate script? It would seem to be more flexible if one could also define an anonymous function as a worker, or do some dynamic coding things before starting a worker (a dependency injection for example). If I understand the spec correctly, this is not possible now, and the reason for this is not made clear (or at least I could not find it).

One possible reason I can think of is the separation between the worker's environment and the host environment, but aren't there other ways to accomplish this?

I assume there are other very good reasons for this, but again, I could not find them written down somewhere.
cheezy
<h6>
 
Posts: 2
Joined: Mon Dec 14, 2009 9:30 am

Re: Workers without separate script

Postby zcorpan » Tue Dec 15, 2009 2:57 pm

cheezy wrote:Why do web workers always have to be defined in a separate script? It would seem to be more flexible if one could also define an anonymous function as a worker, or do some dynamic coding things before starting a worker (a dependency injection for example). If I understand the spec correctly, this is not possible now, and the reason for this is not made clear (or at least I could not find it).

One possible reason I can think of is the separation between the worker's environment and the host environment,
Yep.

cheezy wrote: but aren't there other ways to accomplish this?
How?

cheezy wrote:I assume there are other very good reasons for this, but again, I could not find them written down somewhere.
zcorpan
<article>
 
Posts: 807
Joined: Tue Feb 06, 2007 8:29 pm
Location: Sweden

Re: Workers without separate script

Postby cheezy » Wed Dec 16, 2009 9:15 am

zcorpan wrote:
cheezy wrote:Why do web workers always have to be defined in a separate script? (...) One possible reason I can think of is the separation between the worker's environment and the host environment,
Yep.

Thank you for confirming my guess. Is this noted in a FAQ or some similar place? Or is it so obvious that I am the only one thinking about it?
zcorpan wrote:
cheezy wrote: but aren't there other ways to accomplish this?
How?

I am not a Javascript VM developer, so if the following does not make sense, please don't be too hard on me. A reply of "Sorry, we thought about this longer than you did, and there are still cases where this is impossible" is perfectly valid, but the more I can learn from this conversation, the better.

Would it be possible to do a deep copy of the function (object) you pass to the the constructor? So copy everything (or mark it for copy-on-write), but remove references to DOM elements if they exist. This way, I think you can create a parallel data structure, so the original one remains untouched (avoiding concurrency issues).

The important difference between this and the usual JSON-serializing of objects that the examples talk about, is that functions can be passed through too in an easy manner. If you have to simulate this using only Javascript, you have to somehow bind the free variables, which requires some introspection, and thus is not easy (if even possible?) to simulate in "user space".

The Google Gears API seems to provide both createWorker(scriptText) and createWorkerFromUrl(scriptUrl). Why was only the URL variant retained in the Web Workers spec? With the script variant, there would have been at least a little basis for more dynamic programming.
cheezy
<h6>
 
Posts: 2
Joined: Mon Dec 14, 2009 9:30 am

Postby zcorpan » Wed Dec 16, 2009 12:14 pm

I think the reason the workers spec only allows URLs currently is because of a desire to keep things simple for the first version, and then add stuff later if there's demand.

I agree that it would be nice, and have suggested that same-document workers should be supported:

http://lists.whatwg.org/htdig.cgi/whatw ... 23138.html

editor's response:

http://lists.whatwg.org/pipermail/whatw ... 23588.html

Opera's (not yet publicly available) implementation currently supports data: URLs in the constructors.

Doing a function clone seems a bit scary, but I haven't given it much thought. It would certainly be elegant if it worked. Maybe you could suggest this to the whatwg list?
zcorpan
<article>
 
Posts: 807
Joined: Tue Feb 06, 2007 8:29 pm
Location: Sweden

Re: Workers without separate script

Postby zcorpan » Wed Dec 16, 2009 12:17 pm

cheezy wrote:Thank you for confirming my guess. Is this noted in a FAQ or some similar place? Or is it so obvious that I am the only one thinking about it?
Maybe you could suggest to the WG that the spec should explain this more clearly?
zcorpan
<article>
 
Posts: 807
Joined: Tue Feb 06, 2007 8:29 pm
Location: Sweden


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest