连续写入服务器中的文件速度很慢 [英] Consecutive writings to files in the server are slow

查看:62
本文介绍了连续写入服务器中的文件速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用MEAN堆栈构建一个模仿plunker的简单游乐场:我们在左侧有文件列表和文本区域,在右侧有实时预览.请注意,文件保存在一个临时文件夹中,实时预览是该临时文件夹中的文件注入的 iframe .

I want to build a simple playground with MEAN stack that mimics plunker: we have a list of files and a textarea on the left hand, and a live preview on the right hand. Note that the files are saved in a temporary folder, and the live preview is an iframe injected by the files from that temporary folder.

我已经编码了一些东西.在前端,控制器监视文本区域中文件的修改;每次更改时,都会调用 render ,并发送 $ http.post 来保存所有文件的新版本.

I have coded something. In the front-end, the controller watches on modifications of files in the textarea; each time there is a change, render will be called and it will send a $http.post to save the new version of all the files.

app.controller('Ctrl', ['$scope', 'codeService', function ($scope, codeService) {
    ...
    $scope.$watch('files', function () {
        codeService.render($scope.files)
    }, true);
}]);

app.service('codeService', ['$http', function ($http) {
    this.render = function (files) {
        ...
        var arrayLength = files.length;
        for (var i = 0; i < arrayLength; i++) {
            $http.post('/writeFile', files[i]);
        }
    }
}

在后端:

router.post('/writeFile', function (req, res, next) {
    console.log("router.post /writeFile");
    var file = req.body;
    var fs = require('fs');
    fs.writeFileSync("public/tmp/" + file.name, file.body);
});

我的测试表明,对于第一个修改,确实将其写入服务器中的文件中.但是,对于连续的修改,第二次及以后的写入可能要花费20秒以上的时间.

My tests show that, for the first modification, it is indeed written to the files in the server. However, for consecutive modifications, the 2nd and following writing may take more than 20 seconds EACH.

有人知道什么会减慢写作速度(第一个除外)?

Does anyone know what slows the writings (except for the 1st one)?

另外,我应该调用 $ http.post('/writeFile',files [i])还是写 router.post('/writeFile'... 以异步方式?

Additionally, should I call $http.post('/writeFile', files[i]) or write router.post('/writeFile'... in an asynchronous way?

我还想知道以下列方式编写http请求是否正确(我在同步功能(即 render )内部是否具有异步功能(即http post)?我应该使 render 异步吗?):

I am also wondering if it is correct to write the http request in the following way (am I having an asynchronous function (ie, http post) inside a synchronous function (ie, render)? Should I make render asynchonous?):

app.service('codeService', ['$http', function ($http) {
    this.render = function (files) {
        ...
        var arrayLength = files.length;
        for (var i = 0; i < arrayLength; i++) {
            $http.post('/writeFile', files[i]);
        }
    }
}

当我在代码中看到其他http请求时,这种方式通常是

When I see other http requests in my code, the fashion is often like

o.create = function (post) {
    return $http.post('/posts', post, {
        headers: { Authorization: 'Bearer ' + auth.getToken() }
    }).success(function (data) {
        o.posts.push(data)
    });
};

推荐答案

您可以尝试重构您的代码并包括以下内容:

Your could try to refactor your code and include following things:

1)将您的观察者包装到反跳功能中. https://lodash.com/docs/4.17.4#debounce

1) Wrap your watcher into debounce function.https://lodash.com/docs/4.17.4#debounce

$scope.$watch('files', _.debounce(function () {
        codeService.render($scope.files)
    }, 1000), true);

它可以防止无用的呼叫

It prevents useless calls

2)使用 writeFile 代替 writeFileSync

fs.writeFile("public/tmp/" + file.name, file.body, (err) => {
  if (err) throw err;
  console.log('It\'s saved!');
});

NodeJ在异步功能中的强大功能,请尝试避免在代码中进行同步调用.

The power of NodeJs in async functions, try to avoid sync calls in your code.

这篇关于连续写入服务器中的文件速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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