For an example use case, I'd like to be able to take a function with
bound vars, run it through this magic serialization engine, throw it
into a queue, and have another worker on another machine deserialize
and execute it. Sound interesting?
Happy nodeing,
–Jacob
Free variables?
> run it through this magic serialization engine, throw it
> into a queue, and have another worker on another machine deserialize
> and execute it. Sound interesting?
The reason to use a closure, as opposed to just a self-contained
JavaScript function, is that the closure can modify the state of
variables in the enclosing scope. Once you serialize it, that
presumably won't be the case anymore, so what's the benefit?
var outside = 'world'
, fn = function (argument) { return [argument, outside].join (' ') }
fs.writeFile ('serialized.fn', magicSerializer.serialize (fn))
---
and somewhere else with no shared memory,
fs.readFile ('serialized.fn', function (err, data) {
magicSerializer.deserialize (data) ('hello') //=> 'hello world'
})
Is that more clear?
–Jacob
> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>
Just use .toString(), +, and eval().
Interesting question, yes. After giving it a few minutes thought, I
don't think there is a reliable way to do this. In the example below,
what would you expect to be actually serialized? I pained my mind but
couldn't come up with a good answer.
var foo = 'bar', bar = 'baz';
function x() { return window[foo]; } // should return 'baz'
FYI, SpiderMonkey has this
(http://mxr.mozilla.org/mozilla-central/source/js/src/jsxdrapi.h).
-L
On Jun 8, 2010, at 4:48 PM, Marak Squires wrote:Just use .toString(), +, and eval().That won't save the evaluation context. Is there a native way to get it in node?
Trying to serialize the free variables is a leaky abstraction because
you won't be able to tell when they change, and even if you could
notify the clients of the change you'll hit aweful side effects when
clients are competing for setting the same state in the free space.
Bad, bad, bad idea. Don't do this.
-Mikeal
--
I understand that you wouldn't be able to track changes or make
changes back *into* the original context, but that's ok; it would be a
something like a snapshot of state. In fact, the intent would be to
execute the function with the free variable state at the time of
serialization, even if values have since changed.
I understand there would be things difficult/impossible to serialize,
like file handles & sockets. I must admit I expected those issues to
be what people brought up, but a few limitations like that don't seem
so horrible.
Is this really impossible, or is it just difficult/intimidating?
Thanks for all the feedback,
–Jacob
I find this a facinating idea, despite its spaghetti guideline.I guess, the fact of context serialization must be deterministically driven across the network for reliable re-execution in the context given.
This works great, but I think node could have an even slicker form of
distributed computing.
I believe serializing execution context would allow you to do this
with anonymous functions. You could wrap nearly any block of code in
your app with "distribute (function () { ... })" and it could be sent
to another process or machine. The worker machines wouldn't need to
know anything about your app, because they'd get anything they needed
along with the function.
Locally, forking/worker interfaces could take advantage of this and
instead of forking a particular predefined static file, they could
fork an anonymous function, much like ruby's Kernel.fork. Imagine
"fork (function() { ... })" just magically executing that code in a
subprocess.
–Jacob