问题
node's redis's async nature is killing me.
I see when message handling is switched from one client to another client, variables I believed to be independent looks be overriden by other clients.
I don't know why variable is shared between the two clients.
var subscriber = redis.createClient(port, host);
var client = redis.createClient(port, host);
var requirejs = require('requirejs');
requirejs(['my-function'], function(myFunction) {
subscriber.subscribe('CHANNEL');
subscriber.on('message', function(channel, message) {
// If I do multiple jobs, say multiple db inserts
var data = JSON.parse(message);
var callback = function(result) {
client.publish(result);
};
myFunction(data, callback);
});
});
data from client1 is different from client2, but when I log it data from client2 is logged (sometimes not always)
define([
'module',
'text!my-script.js',
'script-fn',
], function(module, myScript, scriptFn) {
var myFunction = function(data, callback) {
// I pass data to functions taht use vm.createScript, and requirejs
// there I print data
console.log(data);
});
});
I use requirejs/vm , it's very complicated I'm linking my previous question here
node.js redis server, what happens when there are multiple messages to the subscribed channel?
回答1:
I do not really understand the question.
But at the end of the myFunction function, you do not call explicitly the callback, so there is no way client.publish() can be called. If you run this code, I expect messages to be displayed in the console, without publishing anything on the Redis client.
来源:https://stackoverflow.com/questions/33954599/node-redis-variables-are-shared-between-clients