Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript是否等待局部变量的更改?_Javascript_Node.js_Asynchronous_Async Await_Port - Fatal编程技术网

Javascript是否等待局部变量的更改?

Javascript是否等待局部变量的更改?,javascript,node.js,asynchronous,async-await,port,Javascript,Node.js,Asynchronous,Async Await,Port,有人能解释我在Javascript中使用异步函数的错误吗 基本上,我必须在Node.js代码中使用一个异步端口来获取一个开放端口供我使用。在异步调用之外设置了一个局部变量,我可以很好地访问/使用它,直到我等待异步函数返回为止。之后,局部变量未定义 (async () => { console.log("CHECK AFTER ASYNC1: " + csvFilePath); // First, grab a valid open port var port;

有人能解释我在Javascript中使用异步函数的错误吗

基本上,我必须在Node.js代码中使用一个异步端口来获取一个开放端口供我使用。在异步调用之外设置了一个局部变量,我可以很好地访问/使用它,直到我等待异步函数返回为止。之后,局部变量未定义

(async () => {
    console.log("CHECK AFTER ASYNC1: " + csvFilePath);
    // First, grab a valid open port
    var port;
    while (!port || portsInProcess.indexOf(port) >= 0) {
        console.log("CHECK AFTER ASYNC2: " + csvFilePath);
        port = await getPort();
        console.log(port);
    }
    console.log("CHECK AFTER ASYNC3: " + csvFilePath);
    portsInProcess.push(port);
    // ... more code below...
对于csvFilePath变量,检查#1和2没有问题,但检查#3显示它未定义。但是,端口号可以。这让我相信Javascript中的异步函数调用存在一些奇怪之处,这些调用只影响局部变量;我进一步使用的全局变量很好。不幸的是,在这里,我无法使csvFilePath变量成为全局变量,因为这也会在该变量上引入竞争条件(我在别处阻止了这种情况;while循环是为了帮助防止端口号上的竞争条件,在我对localhost的简单测试中基本上没有使用这种情况)

为了以防万一,我得到的结果如下:

CHECK AFTER ASYNC1: data/text/crescent_topics.csv
CHECK AFTER ASYNC2: data/text/crescent_topics.csv
58562
CHECK AFTER ASYNC3: null
可能还值得一提的是,实际上只有前几行代码可以动态获取开放端口,这就是我添加的代码行。我之前使用固定端口号的代码运行良好(包括保持稳定的csvFilePath变量)

我对
await
功能的理解是,它使异步函数或多或少地同步运行,这就是这里发生的事情;我下面使用端口号的代码在设置端口号之前不会运行。(但即使不是这样,为什么csvFilePath变量没有设置,因为我在这里没有改变它或以任何方式使用它?)

编辑:这里还有一些代码可以提供额外的上下文

var spawn = require('child_process').spawn;
var fs = require("fs");
var async = require('async');
var zmq = require('zmq');
var readline = require('readline');
const getPort = require('get-port');

/* Export the Nebula class */
module.exports = Nebula;

/* Location of the data for the Crescent dataset */
var textDataPath = "data/text/";
var crescentRawDataPath = textDataPath + "crescent_raw";
var crescentTFIDF = textDataPath + "crescent tfidf.csv";
var crescentTopicModel = textDataPath + "crescent_topics.csv";

/* Location of the data for the UK Health dataset */
var ukHealthRawDataPath = textDataPath + "uk_health_raw";
var ukHealthTFIDF = textDataPath + "uk_health.csv";

/* Map CSV files for text data to raw text location */
var textRawDataMappings = {};
textRawDataMappings[crescentTFIDF] = crescentRawDataPath;
textRawDataMappings[crescentTopicModel] = crescentRawDataPath;
textRawDataMappings[ukHealthTFIDF] = ukHealthRawDataPath;
textRawDataMappings[textDataPath + "uk_health_sm.csv"] = ukHealthRawDataPath;

/* The pipelines available to use */
var flatTextUIs = ["cosmos", "composite", "sirius", "centaurus"];
var pipelines = {
    andromeda: { 
        file: "pipelines/andromeda.py",
        defaultData: "data/highD/Animal_Data_study.csv"
     },
     cosmos: {
        file: "pipelines/cosmos.py",
        defaultData: textDataPath + "crescent tfidf.csv"
     },
     sirius: {
        file: "pipelines/sirius.py",
        defaultData: "data/highD/Animal_Data_paper.csv"
     },
     centaurus: {
        file: "pipelines/centaurus.py",
        defaultData: "data/highD/Animal_Data_paper.csv"
     },
     twitter: {
        file: "pipelines/twitter.py",
     },
     composite: {
        file: "pipelines/composite.py",
        defaultData: textDataPath + "crescent tfidf.csv"
     },
     elasticsearch: {
        file: "pipelines/espipeline.py",
        args: []
     }
};

/* The locations of the different types of datasets on the server */
var textDataFolder = "data/text/";
var highDDataFolder = "data/highD/";
var customCSVFolder = "data/customCSV/";

var sirius_prototype = 2;

// An array to track the ports being processed to eliminate race conditions
// as much as possible
var portsInProcess = [];

var nextSessionNumber = 0;
var usedSessionNumbers = [];

/* Nebula class constructor */
function Nebula(io, pipelineAddr) {
    /* This allows you to use "Nebula(obj)" as well as "new Nebula(obj)" */
    if (!(this instanceof Nebula)) { 
        return new Nebula(io);
    }

    /* The group of rooms currently active, each with a string identifier
     * Each room represents an instance of a visualization that can be shared
     * among clients.
     */
    this.rooms = {};
    this.io = io;

    /* For proper use in callback functions */
    var self = this;

    /* Accept new WebSocket clients */
    io.on('connection', function(socket) {

    // Skipped some irrelevant Socket.io callbacks

    **// Use the csvFilePath to store the name of a user-defined CSV file
        var csvFilePath = null;**

        /* Helper function to tell the client that the CSV file is now ready for them
        * to use. They are also sent a copy of the data
        */
        var csvFileReady = function(csvFilePath) {

            // Let the client know that the CSV file is now ready to be used on
            // the server
            socket.emit("csvDataReady");

            // Prepare to parse the CSV file
            var csvData = [];
            const rl = readline.createInterface({
                input: fs.createReadStream(csvFilePath),
                crlfDelay: Infinity
            });

            // Print any error messages we encounter
            rl.on('error', function (err) {
                console.log("Error while parsing CSV file: " + csvFilePath);
                console.log(err);
            });

            // Read each line of the CSV file one at a time and parse it
            var columnHeaders = [];
            var firstColumnName;
            rl.on('line', function (data) {                
                var dataColumns = data.split(",");

                // If we haven't saved any column names yet, do so first
                if (columnHeaders.length == 0) {
                    columnHeaders = dataColumns;
                    firstColumnName = columnHeaders[0];
                }

                // Process each individual line of data in the CSV file
                else {
                    var dataObj = {};
                    var i;
                    for (i = 0; i < dataColumns.length; i++) {
                        var key = columnHeaders[i];
                        var value = dataColumns[i];
                        dataObj[key] = value
                    }
                    csvData.push(dataObj);
                }

            });

            // All lines are read, file is closed now.
            rl.on('close', function () {

                // On certain OSs, like Windows, an extra, blank line may be read
                // Check for this and remove it if it exists
                var lastObservation = csvData[csvData.length-1];
                var lastObservationKeys = Object.keys(lastObservation);
                if (lastObservationKeys.length = 1 && lastObservation[lastObservationKeys[0]] == "") {
                    csvData.pop();
                }

                // Provide the CSV data to the client
                socket.emit("csvDataReadComplete", csvData, firstColumnName);
            });
        };

        **/* Allows the client to specify a CSV file already on the server to use */
        socket.on("setCSV", function(csvName) {
            console.log("setCSV CALLED");
            csvFilePath = "data/" + csvName;
            csvFileReady(csvFilePath);
            console.log("CSV FILE SET: " + csvFilePath);
        });**

        // Skipped some more irrelevant callbacks

        /*  a client/ a room. If the room doesn't next exist yet,
        * initiate it and send the new room to the client. Otherwise, send
        * the client the current state of the room.
        */
        socket.on('join', function(roomName, user, pipeline, args) {
            console.log("Join called for " + pipeline + " pipeline; room " + roomName);
            socket.roomName = roomName;
            socket.user = user;
            socket.join(roomName);

            console.log("CSV FILE PATH: " + csvFilePath);

            var pipelineArgsCopy = [];

            if (!self.rooms[roomName]) {
                var room = {};
                room.name = roomName;
                room.count = 1;
                room.points = new Map();
                room.similarity_weights = new Map();

                if (pipeline == "sirius" || pipeline == "centaurus") {
                    room.attribute_points = new Map();
                    room.attribute_similarity_weights = new Map();
                    room.observation_data = [];
                    room.attribute_data = [];
                }

                /* Create a pipeline client for this room */
                console.log("CHECK BEFORE ASYNC: " + csvFilePath);
                **// Here's the code snippet I provided above**
                **(async () => {
                    console.log("CHECK AFTER ASYNC1: " + csvFilePath);
                    // First, grab a valid open port
                    var port;
                    while (!port || portsInProcess.indexOf(port) >= 0) {
                        console.log("CHECK AFTER ASYNC2: " + csvFilePath);
                        port = await getPort();
                        console.log(port);
                    }
                    console.log("CHECK AFTER ASYNC3: " + csvFilePath);**
                    portsInProcess.push(port);
                    console.log("CHECK AFTER ASYNC4: " + csvFilePath);

                    if (!pipelineAddr) {
                        var pythonArgs = ["-u"];
                        if (pipeline in pipelines) {

                            // A CSV file path should have already been set. This
                            // file path should be used to indicate where to find
                            // the desired file
                            console.log("LAST CHECK: " + csvFilePath);
                            if (!csvFilePath) {
                                csvFilePath = pipelines[pipeline].defaultData;
                            }
                            console.log("FINAL CSV FILE: " + csvFilePath);
                            pipelineArgsCopy.push(csvFilePath);

                            // If the UI supports reading flat text files, tell the
                            // pipeline where to find the files
                            if (flatTextUIs.indexOf(pipeline) >= 0) {
                                pipelineArgsCopy.push(textRawDataMappings[csvFilePath]);
                            }

                            // Set the remaining pipeline args
                            pythonArgs.push(pipelines[pipeline].file);
                            pythonArgs.push(port.toString());
                            if (pipeline != "twitter" && pipeline != "elasticsearch") {
                                pythonArgs = pythonArgs.concat(pipelineArgsCopy);
                            }
                        }
                        else {
                            pythonArgs.push(pipelines.cosmos.file);
                            pythonArgs.push(port.toString());
                            pythonArgs.push(pipelines.cosmos.defaultData);
                            pythonArgs.push(crescentRawDataPath);
                        }

                        // used in case of CosmosRadar
                        for (var key in args) {
                            if (args.hasOwnProperty(key)) {
                                pythonArgs.push("--" + key);
                                pythonArgs.push(args[key]);
                            }
                        }

                        // Dynamically determine which distance function should be
                        // used
                        if (pythonArgs.indexOf("--dist_func") < 0) {
                            if (pipeline === "twitter" || pipeline === "elasticsearch" ||
                                    csvFilePath.startsWith(textDataPath)) {
                                pythonArgs.push("--dist_func", "cosine");
                            }
                            else {
                                pythonArgs.push("--dist_func", "euclidean");
                            }
                        }

                        console.log(pythonArgs);
                        console.log("");

                        var pipelineInstance = spawn("python2.7", pythonArgs, {stdout: "inherit"});

                        pipelineInstance.on("error", function(err) {
                            console.log("python2.7.exe not found. Trying python.exe");
                            pipelineInstance = spawn("python", pythonArgs,{stdout: "inherit"});

                            pipelineInstance.stdout.on("data", function(data) {
                                console.log("Pipeline: " + data.toString());
                            });
                            pipelineInstance.stderr.on("data", function(data) {
                                console.log("Pipeline error: " + data.toString());
                            });
                        });

                        /* Data received by node app from python process, 
                         * ouptut this data to output stream(on 'data'), 
                         * we want to convert that received data into a string and 
                         * append it to the overall data String
                         */
                        pipelineInstance.stdout.on("data", function(data) {
                            console.log("Pipeline STDOUT: " + data.toString());
                        });
                        pipelineInstance.stderr.on("data", function(data) {
                            console.log("Pipeline error: " + data.toString());
                        });

                        room.pipelineInstance = pipelineInstance;
                    }

                    /* Connect to the pipeline */
                    pipelineAddr = pipelineAddr || "tcp://127.0.0.1:" + port.toString();

                    room.pipelineSocket = zmq.socket('pair');
                    room.pipelineSocket.connect(pipelineAddr);

                    pipelineAddr = null;
                    portsInProcess.splice(portsInProcess.indexOf(port), 1);

                    /* Listens for messages from the pipeline */
                    room.pipelineSocket.on('message', function (msg) {
                        self.handleMessage(room, msg);
                    });

                    self.rooms[roomName] = socket.room = room;
                    invoke(room.pipelineSocket, "reset");
                })();
            }
            else {
                socket.room = self.rooms[roomName];
                socket.room.count += 1;

                if (pipeline == "sirius" || pipeline == "centaurus") {
                    socket.emit('update', sendRoom(socket.room, true), true);
                    socket.emit('update', sendRoom(socket.room, false), false);
                }
                else {
                    socket.emit('update', sendRoom(socket.room));
                }
            }

            // Reset the csvFilePath to null for future UIs...
            // I don't think this is actually necessary since 
            // csvFilePath is local to the "connections" message,
            // which is called for every individual room
            csvFilePath = null;
        });

        // Skipped the rest of the code; it's irrelevant
    });
}
由于粗体代码不起作用,只需搜索“**”即可找到我标记的相关部分


TL;DR客户机和服务器之间进行了大量通信,以建立直接链接到特定数据集的个性化通信。用户可以将自定义CSV文件上载到系统,但我现在使用的代码只是尝试选择服务器上现有的CSV文件,因此我省略了自定义CSV文件的回调。选择文件后,客户机要求“加入”一个房间/会话。我现在处理的案例假设这是一个新的房间/会话,而不是尝试与另一个客户机共享房间/会话。(是的,我知道,共享房间/会话的代码很混乱,但目前大部分代码都可以使用,这不是我主要关心的问题。)同样,在添加异步代码之前(使用静态端口变量),所有这些代码都工作得很好,所以我不知道通过添加它,什么改变了这么多。

在一些愚蠢的测试之后,我意识到我在异步调用之外将
csvFilePath
重置为
null
,这就是导致错误的原因。。。哎呀

因为您现在包含了整个代码上下文,所以我们可以看到问题在于您的
async
iLife之后的代码是导致问题的原因

async
函数在点击
wait
时立即返回承诺。当
await
正在等待其异步操作时,调用
async
函数后的代码将运行。在您的情况下,您基本上是这样做的:

var csvFilePath = someGoodValue;

(async () => {
     port = await getPort();
     console.log(csvFilePath);    // this will be null
})();

csvFilePath = null;               // this runs as soon as the above code hits the await
因此,只要您第一次点击
wait
async
函数就会返回一个承诺,随后的代码继续运行,点击重置
csvFilePath
的代码行


可能有更干净的方法来重组代码,但您可以做一件简单的事情:

var csvFilePath = someGoodValue;

(async () => {
     port = await getPort();
     console.log(csvFilePath);    // this will be null
})().finally(() => {
    csvFilePath = null;
});
注意:
.finally()
在节点v10+中受支持。如果使用的是旧版本,则可以在
.then()
.catch()
中重置路径


或者,正如您的评论所说,也许您可以完全取消重置
csvFilePath

从问题中的代码中,听起来好像
getPort
正在重新分配
csvFilePath
,尽管这听起来很奇怪-如果不是这样的话,然后,问题中的代码缺少脚本中的代码所具有的某些内容。需要确保
cvsFilePath
在等待期间可能会被函数外部的某个对象重新分配,从而打乱此函数。一个简单的规则是,不要使用可以在异步操作内部修改的共享变量,因为当它们异步执行任务并等待完成时,可能会运行其他东西并更改这些共享变量。也许你应该把它们作为参数传递到这个异步函数中,这样它们就不会从你下面被改变。如果你想要更精确的路径,你需要向我们展示更多的代码,这样我们就可以看到
cvsFIlePath
是在哪里定义的,它是如何使用的,还有什么可以改变它,等等。现在你已经添加了剩下的代码,请使用const,我能准确地看到发生了什么,我在下面的回答中解释了。我昨天意识到我的错误,但你的回答提供了比我想象的更多有用的细节和其他解决问题的方法。谢谢!!:)
var csvFilePath = someGoodValue;

(async () => {
     port = await getPort();
     console.log(csvFilePath);    // this will be null
})().finally(() => {
    csvFilePath = null;
});