0

私は XHR nodejs モジュールを使用して、多くの ajax 呼び出しを行っています (現在は約 100)。私は遅延を使用してこの非同期を行います。これは、要求の量が 40 から 50 の呼び出しのどこかで通過するまで、非常にうまく機能します。その後、単に停止し、無期限に何もしません。同期する場合 (これは実際にはオプションではありません)、それで実行できるすべての呼び出しをサポートします。では、この問題の原因は何ですか?

私のコードは次のとおりです。

    function getFile(url) {
        var def = deferred()
        ,xhr = new XMLHttpRequest(); 

        xhr.open("GET", url); 
        xhr.onreadystatechange = handler;
        //Callback
        function handler (){
            // If status is ready
            if (this.readyState == 4 && this.status >= 200 && this.status < 300 || this.status === 304) {
                try {
                    var result = eval(this.responseText);
                } catch (e) {
                    def.resolve(e);
                }
                def.resolve(result && result[0]);
            } else if (this.status === 401){
                console.log('Error 401')
                def.resolve(new Error('Error:' + this.responseText));
            };
        };
        xhr.send(null);
        return def.promise;
    };

ステータスを console.log にすると、ナイス ステータス 200 が得られます。

次のコード スニペットで getfile() を呼び出しています。

var express = require('express')
, app = express()
, fs = require('fs')
, XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest
, promisify = require('deferred').promisify
, deferred = require('deferred')
, readdir = promisify(fs.readdir)
, exists = promisify(fs.exists)
, writeFile = promisify(fs.writeFile)
, filepath = './public/movies/config/moviefiles.js'

    // Set max of events to unlimited
    req.setMaxListeners(0)
    readdir(configfileResults.moviepath).map(function (file) {
        var filename = file
        , year = longregex
        , movietitle = verylongregex
        if (year == null) year = ''
        var url = "http://api.themoviedb.org/2.1/Movie.search/"+configfileResults.language+"/json/apikey/"+movieTitle+"?year="+ year +"?="
        return getFile(url)(function (ajaxResult) {
            return {
                movieTitle:movieTitle,
                filename: file,
                movieScraperInfo: ajaxResult
            };
        });
    })(function (data) {
       return writeFile(filepath, JSON.stringify(data, null, 4));
    }).end(function () {
        // Download Cache
        downloadCache()
    }, function (e) {
        console.log("Failed", e);
    });

どうもありがとう!

編集:

Brad が提案したように、多くのコンソール ログを追加しました。

function getFile(url) {
    var def = deferred()
    ,xhr = new XMLHttpRequest(); 
    console.log("url:", url);
        xhr.onreadystatechange = function() {
            console.log("status ready");
            if (this.readyState == 4 && this.status >= 200 && this.status < 300 || this.status === 304) {
                try {
                    var result = eval(this.responseText);
                    console.log('getting files', i++)
                } catch (e) {
                    def.resolve(e);
                }
                console.log("resolving files");
                def.resolve(result && result[0]);
            } else if (this.status === 401){
                console.log('Error 401')
                def.resolve(new Error('Error:' + this.responseText));
            };
            console.log("not ready");
        };
        console.log("out of callback");
    xhr.open("GET", url); 
    xhr.send(null);
    return def.promise;
};

結果:

データを取得するために必要なすべての URL を示す 60 個の「url:」という URL ログを取得します。次に、コールバックが実行されています。

その結果:

ファイルの取得 3 ファイルの解決中のステータス 準備中のステータス 準備中のステータス 準備中のステータス ファイルの取得 4 ファイルの解決中のファイルの準備中のステータス 準備中のステータス 準備中のステータス 準備中のステータス 準備完了 ファイルの取得 5 ファイルの解決中のステータス 準備中のステータス 準備中のステータス 準備中のステータスファイルを取得する準備ができました 6 ファイルの解決中の準備ができていないステータス 準備ができていないステータス 準備ができていないステータス ファイルを取得する準備ができていますステータス 準備完了 ファイルを取得中 9 ファイルの解決中 準備中ステータス 準備中 準備中ステータス 準備中ステータス 準備中 ファイルを取得中 10 ファイルの解決中 準備中ステータス 準備中 準備中ステータス 準備中ステータス 準備中 ステータス ファイルを取得中 11 ファイルの解決中 準備中ステータス準備ができていない状態 準備ができていない状態 準備ができているファイルを取得中 12 ファイルの解決中の準備ができていない状態 準備ができていない状態 準備ができていない状態 準備ができていない状態 準備ができていないファイルを取得中 13ステータス 準備ができていません ステータス 準備ができていません ステータス 準備ができています ファイルを取得しています準備完了ステータス 準備完了ステータス 準備完了ステータス 準備完了ファイルの解決 18 ファイルの解決中 準備完了ステータス 準備完了ステータス 準備完了ステータス 準備完了ファイルの解決 19 ファイルの解決中 準備完了ステータス 準備完了ステータス 準備未完了準備完了ステータス 準備完了 ファイルを取得中 20 ファイルの解決中 準備中ステータス 準備中ステータス 準備中ステータス 準備中ステータス ファイルを取得中 21 ファイルの解決中 準備中ステータス 準備中ステータス 準備中ステータス 準備中ステータス ファイルを取得中 22 ファイルの解決中 準備中ステータス 準備中ステータス 準備完了準備ができていないステータス 準備ができていないステータス ファイルを取得中 23 ファイルの解決中 準備ができていないステータス 準備ができていないステータス 準備ができていないステータス 準備ができていないステータス ファイルを取得中 24 ファイルを解決中 準備ができていないステータス 準備ができていないステータス準備完了ステータス 準備完了 ファイルを取得中 26 ファイルの解決中 準備中ステータス 準備中ステータス 準備中ステータス 準備中ステータス 準備中ステータス 準備完了 ファイルを取得中28 ファイルの解決中のステータス 準備中のステータス 準備中のステータス 準備中のステータス 準備中のステータス 準備中のファイルの取得ファイル 31 ファイルの解決中のステータス 準備中のステータス 準備中のステータス 準備中のステータス 準備中のステータス ファイルの解決 32 ファイルの解決中のステータス 準備中のステータス 準備中のステータス 準備中のステータス 準備完了の取得 ファイル 33ファイルの取得 34 ファイルの解決中のステータス 準備中のステータス 準備中のステータス 準備中のステータス 準備完了 ファイルの取得 35 ファイルの解決中のステータス 準備中のステータス 準備中のステータス 準備中のステータス 準備完了 ファイルの取得 36 ファイルの解決中のステータス準備完了ステータス 準備完了ステータス 準備完了 ファイルの解決 準備完了ステータス 準備完了ステータス 準備完了ステータス 準備完了ファイルの解決 38 ファイルの解決 準備完了ステータス 準備完了ステータス 準備完了ステータス 準備完了ファイルの解決 39 ファイルの解決中status ready not ready status ready not ready status ready ファイルの取得 40 ファイルの解決 not ready

「準備ができていません」で終わります。

誰でもこれに光を当てることができますか?github のコードを参照してください。

https://github.com/jansmolders86/mediacenterjs/blob/master/apps/movies/index.js

4

2 に答える 2

1

私は問題が何であるかを理解しました。同時に多くのことを行っていたので、データをメモリに保持しながら他の関数​​に渡していました。つまり、単にメモリが不足しています。コードをリファクタリングします。

于 2013-03-29T15:59:40.107 に答える
0

あなたの構文は意味がありません。ここで何が起こっているのか明確にできますか?

return getFile(url)(function (ajaxResult) {
        return {
            movieTitle:movieTitle,
            filename: file,
            movieScraperInfo: ajaxResult
        };
    });
于 2013-03-19T17:24:51.010 に答える