我正在創建一個專案,我需要在其中接受用戶輸入 - 通過函式將其傳遞并將新值回傳給用戶 - 看起來很簡單。我是異步函式的新手,并且已經閱讀了我可能閱讀的所有內容,如果我遺漏了一個更基本的問題,我將無法解決。我將展示基本代碼,然后是我希望實作的目標。我認為問題在于我回傳的是函式的狀態而不是值,但就是無法解決。
基本代碼:
ipcMain.on('gpt3', (event, args) => {
async function gpt3(args) {
generateResponse('james', 'hello world'); // Takes a user's name & input and recieves a response from a python file.
event.reply('textRecieve', 'hello world'); // Sends 'Hello World' to the user (ipcRenderer 'textRecieve')
}
gpt3(args);
})
async function generateResponse(name, text) {
let testshell = new PythonShell('./python/text_echo.py', { mode: 'text', args: [name, text]});
let content = "";
try {
testshell.on('message', function (message) {
console.log(message); // prints the output from the python file 'Python File: james Text: hello world'
return message; // attempting to return the 'Message' from the python file
});
} catch (error) {
console.log("You've f*cked it somewhere my friend");
console.log(error);
}
}
Python腳本:
import sys
name = sys.argv[1]
text = sys.argv[2]
print(f'Python File: {name} Text: {text}')
sys.stdout.flush()
回報:(如預期)
> Executing task: npm run start <
> electron-quick-start@1.0.0 start
> electron .
Python File: james Text: hello world
我想要它做什么:
ipcMain.on('gpt3', (event, args) => {
async function gpt3(args) {
message = generateResponse('james', 'hello world'); // Takes a user's name & input and recieves a response from a python file, retunring the message to the 'message' variable.
console.log(message);
event.reply('textRecieve', 'message would send here'); // Sends the 'Message' to the user (ipcRenderer 'textRecieve')
}
gpt3(args);
})
async function generateResponse(name, text) {
let testshell = new PythonShell('./python/text_echo.py', { mode: 'text', args: [name, text]});
let content = ""
try {
testshell.on('message', function (message) {
console.log(message); // prints the output from the python file 'Python File: james Text: hello world'
return message; // attempting to return the 'Message' from the python file
});
} catch (error) {
console.log("You've f*cked it somewhere my friend")
console.log(error)
}
return content; // content needs to be message instead due to async nature it returns empty string
}
回報:
> Executing task: npm run start <
> electron-quick-start@1.0.0 start
> electron .
Promise { '' }
Python File: james Text: hello world
TLDR;我想將通過“generateResponse()”生成的“訊息”傳遞給我的“event.reply()”。相反,我收到了我認為是 Promise 的狀態。任何幫助將不勝感激。謝謝
uj5u.com熱心網友回復:
您應該首先解決承諾。
ipcMain.on('gpt3', (event, args) => {
async function gpt3(args) {
const message = await generateResponse('james', 'hello world');
console.log(message);
event.reply('textRecieve', 'message would send here'); // Sends the 'Message' to the user (ipcRenderer 'textRecieve')
}
gpt3(args);
})
async function generateResponse(name, text) {
let testshell = new PythonShell('./python/text_echo.py', { mode: 'text', args: [name, text]});
let content = ""
try {
testshell.on('message', function (message) {
console.log(message); // prints the output from the python file 'Python File: james Text: hello world'
content = message;
});
} catch (error) {
console.log("You've f*cked it somewhere my friend")
console.log(error)
}
return content; // content needs to be message instead due to async nature it returns empty string
}
uj5u.com熱心網友回復:
好的,所以這里有一些問題......但主要是 node.js 在“異步”時“無法”傳遞變數。node.js 對我來說是新手,我不能撒謊說我很困惑。希望以下指向一個很好的解決方法/方法的鏈接以及我的作業代碼能夠幫助某人:
出于某種原因,我沒有想到函式可以嵌套在這樣的函式中,全部在一行中。對于習慣 JS 或 node.js 的人來說,這可能看起來很基礎,但對于我和其他人來說這是第一次專案——如果仍然使用 python 代碼。希望這可能會有所幫助!
ipcMain.on('gpt3', (event, input) => { gpt3Async(event, input, function(result) { event.reply('textRecieve', result); console.log('gpt3Async: ' result);})})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/498356.html
標籤:javascript Python 节点.js 异步 电子
