简体   繁体   English

PowerShell中的流程vs实例vs Runspace

[英]Process vs Instance vs Runspace in PowerShell

the [Powershell]::create() - method creates a new PowerShell-" Instance " either in the "current or a new runspace". [Powershell]::create() -方法在“当前或新运行空间”中创建一个新的PowerShell-“ 实例 ”。

Can someone explain how the terms process , instance , runspace and (maybe thread ) relate to each other in this regard. 有人可以解释这方面的术语processinstancerunspace和(也许是thread )如何相互关联。 In laymen's terms if possible? 用外行的话说吧?

The terms you have are not interchangeable and do not do the same thing. 您拥有的术语不可互换,并且不会做相同的事情。

Process is program that runs an instruction set. 进程是运行指令集的程序。

Thread is a single running of instructions in a program. 线程是程序中指令的单次运行。

Multi-threading is when multiple instructions are run at the same time. 多线程是指同时运行多个指令。 Each requires a separate thread. 每个都需要一个单独的线程。

Runspace is in the same powershell process but calls a new powershell engine in order to run its code without interfering with the current powershell scripts thread. Runspace在同一Powershell进程中,但是调用新的Powershell引擎以便在不干扰当前Powershell脚本线程的情况下运行其代码。

Instance is a contained running of code. 实例是一个包含的运行代码。 Its a descriptor. 它是一个描述符。

So Here are some examples 所以这是一些例子

I can have a Instance of a Process. 我可以有一个流程实例。 I can have a Instance of a Thread. 我可以有一个线程实例。 I Can have a Instance of a Runspance. 我可以有一个Runspance的实例。

Editing to expand on Answer based on comment 编辑以根据评论扩展为答案

"So in the example ive posted above ([Powershell]::create()), is that an instance of a thread, process or runspace?" “因此,在上面发布的示例ive([Powershell] :: create())中,这是线程,进程或运行空间的实例吗?”

So we have a Powershell Application. 因此,我们有一个Powershell应用程序。 What is happening is this application starts a Runspace where your commands will be executed and sets up a location to create Powershell Objects. 发生的情况是此应用程序启动了一个运行空间,在该空间中将执行您的命令,并设置一个位置来创建Powershell对象。 Every time you open the powershell console you are starting another runspace. 每次打开Powershell控制台时,您都在启动另一个运行空间。

The [Powershell]::create() it creates an object where you can determine what will be run and what runspace it will be run on. [Powershell]::create()创建一个对象,您可以在其中确定将要运行的对象以及将在其上运行的运行空间。 If you dont choose a runspace then it will create one for you. 如果您不选择一个运行空间,那么它将为您创建一个。

So [Powershell] is the What will run? 那么[Powershell] 将运行什么? (The Script) and the Where it will run (A Runspace) (脚本)及其运行位置运行空间)

The Runspace is the How will it run? Runspace是如何运行的? (On a powershell Engine) (在powershell引擎上)

You can think of [Powershell]::Create() as a new powershell session on the separate thread. 您可以将[Powershell] :: Create()视为单独线程上的新powershell会话。 This session will have some default runspace created but you can change it to another one. 该会话将创建一些默认运行空间,但是您可以将其更改为另一个运行空间。 Unlike Start-Process (separate process) and Start-Job (child process), [Powershell]::Create() is running on the same process with your main script and sharing memory space with it. 与Start-Process(单独的进程)和Start-Job(子进程)不同,[Powershell] :: Create()与您的主脚本在同一进程上运行,并与其共享内存空间。 This means that you can exchange actual .net objects between the main and child sessions. 这意味着您可以在主会话和子会话之间交换实际的.net对象。 If sessions run on separate processes they can only exchange with textual/serialized data. 如果会话在单独的进程上运行,则只能与文本/序列化数据交换。

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

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