是否需要在Node.js中清除Buffer?如果是,怎么样?如果不是,这里有什么问题? [英] Is it required to clear Buffer in Node.js? If yes, How? If No, what is the issue here?

查看:206
本文介绍了是否需要在Node.js中清除Buffer?如果是,怎么样?如果不是,这里有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作缩略图生成器,我正在使用 Gearman 在后台完成工作照片已上传。我有一个用Node.js编写的工人,它基本上进行缩略图处理(使用 imagemagick 作为节点)并将其插入数据库。客户端是用PHP编写的,它基本上发送 Photo_id 和原始图像的base64编码字符串。 Node.js工作者获取输入base64编码的图像,并对其进行处理。一切都很好。我发现的问题是,当我通过Node调用Node.js工作时。 PHP第一次生成所有3个缩略图(我为每个图像生成3个缩略图),第二次和第三次生成所有3个缩略图,但是第四次只生成一个缩略图,然后生成无论我多少次调用这个工人,都没有生成缩略图。

I am making a thumbnail generator, and I am using Gearman to do the job in the background as soon as a photo is uploaded. I have a worker written in Node.js which basically does the thumbnail processing(using imagemagick for node) and inserting it into the database. The Client side is written in PHP, where it basically send the Photo_id and the base64 encoded string of the original image. The Node.js worker takes the input base64 encoded image, and processes it. Everything is working fine. The issue that I found out was that, when I call the Node.js worker via. PHP the first time, all 3 thumbnails(I am generating 3 thumbnails for every image) are generated, on the second time and third time as well also all 3 are generated, but on the fourth time only one thumbnail is generated, and after than the no matter how many times I call the worker, no thumbnail is getting generated.

我认为问题可能是缓冲区被填满,因为我正在使用Node .js缓冲区将base64字符串转换为Binary,反之亦然。

What I think the issue might be is the Buffer getting filled, as I am using Node.js Buffer to convert base64 string to Binary and vice versa.

我搜索了清除缓冲区的方法,比如将缓冲区变量指向null ,在缓冲区变量上使用js delete 函数。但是,这似乎都没有帮助。缓冲区未被清除或问题根本没有缓冲区。

I searched for ways to clear the buffer, like pointing the buffer variable to null, using js delete function on the buffer variable. But, none of this seemed to help. Either the buffer is not getting cleared or the issue is not with the buffer at all.

请仔细阅读代码,并告诉我可能出现的问题。

Please go through the code, and let me know what the issue might be.

PHP Gearman客户

<?php
$client= new GearmanClient();
$client->addServer('127.0.0.1',4731);
print $client->do("infinite", "[\"12246\", \"Base 64 image string in here\"]");
?>

Node.js Gearman客户端

var fs = require('fs');
var db = require('./class.photo.js');
var im = require('imagemagick');
var Gearman = require('node-gearman/lib/gearman.js');
var gearman = new Gearman("127.0.0.1", 4731);
var db = new db();

function initializePic(id,base, req_type, callback) {
    console.log("Initializing thumbnail resize on " + id);

        var rawbase = base.split(",")[1];
        rawbase = rawbase.replace("\n", "");
        var buff = new Buffer(rawbase, 'base64');
        console.log(buff.length);
        var bin = buff.toString('binary');
        buff = null;

        console.time("image_" + req_type);
        imageResize(id, bin, properties[req_type].img_dest_width, properties[req_type].img_dest_height, imageRequestEnum[req_type], function (res) {
            if (res) {
                console.timeEnd(req_type);
                console.timeEnd("image_" + req_type);
            }
        });
}

function imageResize(id, bin, w, h, s, callback) {
    var fileName = id + '_' + w + '_' + h + '_thumb.jpg'
    console.log("Initializing resize process with filename " + fileName);
    im.resize({
        srcData: bin,
//        dstPath: fileName,
        width: w,
        height: h + '\!',
        quality: 0.7,
        strip: false,
        progressive: true,
    }, function (err, stdout, stderr) {
        if (err)
            throw err;
        if (stderr)
            throw stderr;

        var buff = new Buffer(stdout, "binary");
        console.log(buff.length);
        var b64 = buff.toString('base64');
        buff = null;

        if (b64) {
            b64 = "data:image/jpeg;base64," + b64;
            db.insertThumb(id, b64, s, function (res) {
                if (res) {
                    callback(true);
                }
            });
        }
    });
}


gearman.connect();

gearman.registerWorker("infinite", function (payload, worker) {
    var payload = payload.toString();
    var json = JSON.parse(payload);
    var id = json[0]; //getting the photo_id from the payload
    var base = json[1]; //Getting the base64 image data from the payload

    worker.end();
    console.time("user_image_small");
    initializePic(id,base, "user_image_small");
    console.time("user_image_medium");
    initializePic(id,base, "user_image_medium");
    console.time("profile_photo");
    initializePic(id,base, "profile_photo");
});

Node.js控制台响应

Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
profile_photo: 151ms
image_profile_photo: 150ms
user_image_medium: 211ms
image_user_image_medium: 210ms
user_image_small: 218ms
image_user_image_small: 217ms
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
user_image_small: 98ms
image_user_image_small: 96ms
profile_photo: 142ms
image_profile_photo: 141ms
user_image_medium: 147ms
image_user_image_medium: 145ms
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
user_image_small: 90ms
image_user_image_small: 89ms
user_image_medium: 141ms
image_user_image_medium: 140ms
profile_photo: 138ms
image_profile_photo: 137ms
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
user_image_small: 94ms
image_user_image_small: 93ms
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246

请注意,在最近几次尝试中,它实际上并没有创建任何缩略图。显示所花费的时间基本上意味着缩略图创建过程已完成。

Notice in the last few tries, its not actually creating any thumbnail. Displaying the time taken basically means the thumbnail creation process is complete.

我不确定问题可能是什么。寻求一些帮助。

I am not sure what the issue might be. Looking for some help.

推荐答案

经过多次故障排除后,发现问题出在 node-mysql 包。使用node.js的 mysql 包解决了这个问题。检查我的其他问题和答案了解更多详情。

After much troubleshooting, found out that the issue was with the node-mysql package. Using the mysql package for node.js solved the issue. Check my other question and answer for more details.

这篇关于是否需要在Node.js中清除Buffer?如果是,怎么样?如果不是,这里有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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