首页 > 解决方案 > node-celery 任务总是返回 null

问题描述

我有一个使用 node-celery 的 JavaScript 程序,这是一个调用 celery 任务并获取结果的模块。我试图通过从“添加”芹菜任务中获得结果来开始简单。我正在遵循文档中的确切演示代码,但仍然没有返回任何结果(请参阅此链接上的文档:https ://github.com/mher/node-celery (查看“后端”下的第一个示例))

芹菜代码:

@app.task(base=QueueOnce)
def add(x, y):
    return x + y

JavaScript

client = celery.createClient({
    CELERY_BROKER_URL: "redis://myurl.com:0000/0",
    CELERY_RESULT_BACKEND: "redis://myurl.com:0000/0"
});

client.on('connect', function() {    //on successful conncection to the celery server
    console.log(client.ready);       //outputs 'true' meaning my code is connecting to the celery server properly  
    var result = client.call('tasks.add', [1, 2]);   //call the 'add' task (shown above) with args as 1 and 2
    setTimeout(function() {
        result.get(function(data) {  //when task returns result. . .
            console.log(data);       //output result; data will be null if the task is not finished by timeout
        });
    }, 10000);   //time to time out if celery is taking too long (should not take anywhere near this long)
});

我的问题是它console.log(data); 总是返回 null,这意味着我的 JS 永远不会得到 celery 的响应。

这确实是一种奇怪的行为,表明我正在遵循文档中的确切示例,并且我还通过不仅console.log(client.ready);在我的代码中使用而且我还尝试将CELERY_BROKER_URLand更改为CELERY_RESULT_BACKEND导致连接失败的不同值来验证它是否正确连接 celery 服务器. 我还将超时时间设置为最大值,它仍然返回 null 所以这不是超时问题。

这里可能是什么问题?任何帮助将不胜感激!

标签: javascriptnode.jscelerynode-celery

解决方案


推荐阅读