在Electron中渲染器与主要过程之间的区别 [英] Distinction between the renderer and main processes in Electron

查看:112
本文介绍了在Electron中渲染器与主要过程之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我本来以为Electron中的渲染器过程是在类似chrome的环境中沙盒化的,这意味着您只能做的就是DOM。但是,我最近了解到,您可以访问文件系统,运行子进程并获取其输出以及导入所需的任何其他节点模块。

I originally thought that the renderer process in Electron was sandboxed in a chrome-like environment, meaning all you can do is mess with the DOM. However, I recently learned that you can access the filesystem, run child processes and get their output, and import any other node modules you want.

如果是这种情况,那么主进程和渲染器进程之间到底有什么区别?这不是硬性分离吗?

If this is the case, what exactly is the distinction between the main process and the renderer process? Is it not a hard separation? What kind of code goes in the main process and what kind of code goes in the renderer process?

如果有人对Electron应用程序体系结构有很好的深入阅读/演示,那么在主过程中将使用哪种代码?在渲染器过程中将使用哪种代码?我也想看看这个。可能有助于消除一些困惑

If anyone has a good in-depth reading/presentation on Electron app architecture I would love to look at that too; might help clear up some of the confusion

推荐答案

主要/渲染过程的区别实际上并不是电子概念本身-它是从Chromium继承的(关于Chromium的体系结构和相关内容,这是文章背后的推理)。这是Chrome出于性能和稳定性原因而使用的体系结构。每个webContents实例都在其自己的进程(渲染器进程)中运行。除其他事项外,主要流程(只能是其中之一)管理webContents实例。

The main/renderer process distinction isn't actually an Electron concept per se -- it's inherited from Chromium (here's an article on Chromium's architecture and the reasoning behind it). This is the architecture that Chrome uses for performance and stability reasons. Each webContents instance runs in it's own process (a "renderer" process). The main process (there can only be one of these) manages webContents instances, among other things.

有一个在此处很好地讨论了两者之间的区别。

There's a good discussion here on the differences between the two.

某些API仅在一个或另一个进程中可用,这可以帮助您了解逻辑在哪里。例如,只能从渲染器进程创建通知(使用HTML5接口,但已实现为本机通知)。 菜单类只能在主菜单中调用处理。仔细阅读Electron模块的API文档,看看结果如何。您可以使用 IPC 远程模块,或 electron-remote 来协调两个过程(您使用哪个过程取决于您的用例)。

Some APIs are only available in one process or the other, which can help you get a sense of what logic goes where. For example, Notifications (uses the HTML5 interface but are implemented as native notifications) can only be created from a renderer process. The Menu class can only be called from within the main process. Read through the Electron modules' API docs and see what goes where. You can use IPC, remote module, or electron-remote to coordinate between the two processes (which one you use depends on your use case).

我会说这是一个硬分离。它们都是独立的过程,因此不共享任何资源或状态。对于我认为的大多数JS开发人员来说,这是一个范式转变(至少对我而言)。例如,如果我有一个有状态模块,该模块在主进程中设置了某个状态,然后我需要在渲染器中使用该模块,则该状态将不存在。它们是该模块的两个完全不同的实例。在主进程中最好共享这样的状态,然后使用上述方法之一在渲染器进程之间共享该状态。

I would say it is a "hard" separation. They're all separate processes and thus don't share any resources or state. This is a paradigm shift for most JS devs I think (at least it was for me). For example if I had a stateful module that I set some state in in the main process, and then I required that module in the renderer, that state won't be there. They're two entirely different instances of that module. Sharing state like that would probably be best in the main process and then either use one of the methods described above to share that state between renderer processes.

这是现实生活中的应用程序的列表一些示例应用。

Shawn Rakowski说得很好(在下面的评论中):将处理平台基础结构代码的代码(例如,创建窗口,注册全局快捷方式等)放在Main流程和特定于应用程序中可能是一个好规则代码(您的应用程序实际在执行的操作)。

Shawn Rakowski said it well (in comments below): "It may be a good rule to put code dealing with the platform infrastructure code (i.e. creating windows, registering global shortcuts, etc) in the Main process and application specific code (what your app is actually doing) in the Renderer processes."


[我的应用程序具有功能]解析一些文件,然后渲染屏幕上的信息

[My app's functionality is it] parses some files and then renders the info to the screen

在Electron中,您可以采用很多方法,因为 fs 模块(以及所有的node.js模块)。

There's lots of approaches you can take to this in Electron because the fs module (and all node.js modules) are available to you in the renderer process.

如果您仅处理一个浏览器窗口实例,并且不进行CPU密集型解析,我会说运行所有 fs 该渲染器流程实例中的相关代码。那是最简单的方法。

If you're only dealing with one browser window instance and not doing CPU intensive parsing, I would say run all the fs related code in that renderer process instance. That's the simplest way.

如果要对这些文件进行CPU密集型工作,则不想锁定UI,这意味着您无法处理浏览器窗口渲染器,而您根本不能这样做(这将锁定所有渲染器!)。因此,我将研究类似电子远程之类的东西,或者创建一个运行繁琐的不可见的浏览器窗口实例起重。

If you're doing CPU intensive work on these files, you don't want to lock up the UI, which means you can't do your processing browser window renderer, and you can't do it in the main (this will lock up all your renderers!). So I would look into something like electron-remote or create an invisible browser window instance that runs the heavy lifting.

有关主进程和渲染器进程对这些主题进行了更深入的讨论(公开:我写的是)。

This article about the main and renderer processes talks about these topics more in-depth (disclosure: I wrote that).

这篇关于在Electron中渲染器与主要过程之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆