node redis,客户端之间共享变量? [英] node redis, variables are shared between clients?

查看:44
本文介绍了node redis,客户端之间共享变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

node 的 redis 异步特性让我很不爽.

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);

    });
});

来自 client1 的数据与 client2 不同,但是当我记录它时,来自 client2 的数据被记录(有时并不总是)

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); 
    });
});

我使用 requirejs/vm ,这很复杂,我在这里链接我之前的问题

I use requirejs/vm , it's very complicated I'm linking my previous question here

节点.js redis server,订阅频道有多条消息怎么办?

推荐答案

我不太明白这个问题.

但是在 myFunction 函数的末尾,您没有显式调用回调,因此无法调用 client.publish().如果您运行此代码,我希望消息会显示在控制台中,而不会在 Redis 客户端上发布任何内容.

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.

这篇关于node redis,客户端之间共享变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆