gpt4 book ai didi

javascript - Electron 应用程序性能 : blocking thread with ipcRenderer. sendSync

转载 作者:行者123 更新时间:2023-12-01 16:32:06 24 4
gpt4 key购买 nike

我有一个用 Electron 构建的音乐播放器。

我遇到了一些我没想到的性能/进程阻塞。我正在尝试为繁重的 IO 操作做一些后台进程。 (确定歌曲时长和专辑封面)

我通过 electron.remote 模块调用方法来做到这一点。
我注意到该应用程序并没有以某种方式异步执行这些操作。

我一直在运行性能工具来检查并看到点击处理程序花费了大量时间。

the click handler

深入挖掘我发现 ipcRenderer.sendSync叫做。

enter image description here

有关于 sendSync 的警告Electron Docs 中的阻塞性质.但是,我自己的代码没有调用它。所以我怀疑远程模块或我的代码中的其他东西导致sendSync被称为。

整个应用程序代码在 Github 上,但 here is an example of electron.remote usage .

要点是这样的:

import {remote} from 'electron'

const fs = remote.require('fs')
const mm = remote.require('musicmetadata')

// read song file, IO
function readMetadata (filePath) {
return new Promise(function (resolve, reject) {
const stream = fs.createReadStream(filePath)
mm(stream, {duration: true}, function (err, metadata) {
// ...
})
})
}

// get metadata for an array of songs
async function refreshSongsDuration (songs) {
const songsMetadata = await Promise.all(songs.map((song) => readMetadata(song.filePath)))

return songs.map((song, index) => {
song.duration = songsMetadata[index].duration
return song
})
}

然后在点击处理程序中,我会有这样的东西:
playArtist (artistID) {
const songs = this.library.getArtistSongs(artistID)
this.playlist.setSongs(songs)
musicPlayer.play()

const shouldGetDuration = songs.some((song) => song.duration === 0)

// This is expected to be asynchronous and non blocking.
if (shouldGetDuration) {
mediaLibrary.refreshSongsDuration(songs)
.then((updatedSongs) => {
this.playlist.set('songs', updatedSongs)
})
}
}

所以,我想这里的简单问题是,我做错了什么导致这些阻塞进程?

最佳答案

https://github.com/electron/electron/blob/master/docs/api/remote.md#remote-objects

Each object (including functions) returned by the remote module represents an object in the main process (we call it a remote object or remote function). When you invoke methods of a remote object, call a remote function, or create a new object with the remote constructor (function), you are actually sending synchronous inter-process messages.



每个远程模块本质上都是同步的。

关于javascript - Electron 应用程序性能 : blocking thread with ipcRenderer. sendSync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49578170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com