简体   繁体   English

工作线程 postMessage() 与命令行命令

[英]Worker thread postMessage() vs command line command

I recently learned about Worker threads in Node JS.我最近了解了 Node JS 中的工作线程。 I was trying to create a worker thread to run Stockfish chess engine in node.js.我试图创建一个工作线程来运行 node.js 中的 Stockfish 国际象棋引擎。

The npm package I am using for this is called stockfish .我为此使用的 npm package 称为stockfish I tried using node-stockfish before this but it was not installing with npm as it was using an older version of the type definition for the "AbortSignal" variable apparently causing compatibility issues.在此之前我尝试使用node-stockfish但它没有使用 npm 安装,因为它使用旧版本的“AbortSignal”变量类型定义显然导致兼容性问题。

For the current npm package that I am using even though I was able to install it successfully, I could find very little documentation on how to use it.对于我正在使用的当前 npm package 即使我能够成功安装它,我也找不到关于如何使用它的文档。 So I tried out a few ideas.所以我尝试了一些想法。

import { Worker } from "worker_threads";

const engine = new Worker("./node_modules/stockfish/src/stockfish.js")
engine.on('message', (data) => console.log(data))
engine.postMessage('position startpos move e2e4 e7e5')
engine.postMessage('go movetime 3000')

Here I tried to run the stockfish.js as a worker thread and send commands to it with the postMessage() function. This however did not work and it gave the following output:在这里,我尝试将 stockfish.js 作为工作线程运行,并使用postMessage() function 向它发送命令。然而,这没有用,它给出了以下 output:

worker.js received unknown command undefined
position startpos move e2e4 e7e5
worker.js received unknown command undefined
go movetime 3000

But I know these commands are valid commands if I run the same js from the command line like so:但是我知道如果我从命令行运行相同的 js,这些命令是有效的命令,如下所示: 正如您所看到的,当我从命令行运行 js 文件时,相同的命令起作用

It might be because I am using the flags --experimental-wasm-threads and --experimental-wasm-simd when I am running it from the command line.这可能是因为我在从命令行运行它时使用了标志--experimental-wasm-threads--experimental-wasm-simd I found this command to run it from the little documentation that was present.我发现这个命令是从现有的小文档中运行的。 But I don't know how to mention these flags when I run it through a worker thread.但是当我通过工作线程运行它时,我不知道如何提及这些标志。

Otherwise it could also be that I don't understand how worker threads work yet and postMessage() is not the same as sending it a command from the command line.否则也可能是我还不明白工作线程是如何工作的,并且postMessage()与从命令行向它发送命令不同。

Any help is greatly appreciated.任何帮助是极大的赞赏。

I switched to using stockfish.wasm library instead.我改为使用stockfish.wasm库。 With this library I was able to achieve what I wanted and I don't need to use any worker threads for now.有了这个库,我能够实现我想要的,我现在不需要使用任何工作线程。 Maybe I can add this to a worker thread if required later.如果以后需要,也许我可以将其添加到工作线程。 Here is a simple example:这是一个简单的例子:

const Stockfish = require("stockfish.wasm")

Stockfish().then((engine) => {
    engine.addMessageListener((output) => {
        console.log(output);
        // Do something with the output data here
    })
    engine.postMessage("uci");
    engine.postMessage("ucinewgame");
    engine.postMessage("position startpos");
    engine.postMessage("go depth 20");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM