如何重用Get-VMMServer连接?
我正在编写一个 C# 应用程序,其 Main() 将启动多个线程,每个线程都会触发 Get-VM commandlet。我为此使用 RunspacePool。
目前,每个线程必须首先触发 Get-VMMServer,然后触发 Get-VM。 Get-VMMServer 大约需要 5-6 秒,从而显着影响性能。下面是代码片段:
static void Main()
{
InitialSessionState iss = InitialSessionState.CreateDefault();
PSSnapInException warning;
iss.ImportPSSnapIn("Microsoft.SystemCenter.VirtualMachineManager", out warning);
RunspacePool rsp = RunspaceFactory.CreateRunspacePool(iss);
rsp.Open();
using (rsp)
{
ClassTest n = new ClassTest();
n.intializeConnection(rsp);
Thread t1 = new Thread(new ThreadStart(n.RunScript));
t1.Start();
Thread t2 = new Thread(new ThreadStart(n.RunScript));
t2.Start();
....
}
....
}
class ClassTest
{
RunspacePool rsp;
public void intializeConnection(RunspacePool _rsp)
{
rsp = _rsp;
}
public void RunScript()
{
PowerShell ps = PowerShell.Create();
ps.RunspacePool = rsp;
ps.AddCommand("Get-VMMServer") AddParameter(...); // Doing this in every thread.
ps.AddCommand("Get-VM").AddParameter("Get-VM", "someVM");
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject result in ps.Invoke())
{
...
}
}
}
Get-VMMServer 连接到 Virtual Machine Manager 服务器(如果连接尚不存在),并从 Virtual Machine Manager 数据库中检索表示该服务器的对象。
我希望每个线程都能重用此连接。
我怎样才能实现这个目标?有没有办法在 Main() 中创建此连接,以便池中的所有运行空间都可以使用它?
I am writing a C# application whose Main() will start multiple threads, each thread firing Get-VM commandlet. I am using RunspacePool for this.
Currently each thread has to first fire Get-VMMServer and then Get-VM. Get-VMMServer takes around 5-6 seconds giving a significant performance hit. Below is the code snippet:
static void Main()
{
InitialSessionState iss = InitialSessionState.CreateDefault();
PSSnapInException warning;
iss.ImportPSSnapIn("Microsoft.SystemCenter.VirtualMachineManager", out warning);
RunspacePool rsp = RunspaceFactory.CreateRunspacePool(iss);
rsp.Open();
using (rsp)
{
ClassTest n = new ClassTest();
n.intializeConnection(rsp);
Thread t1 = new Thread(new ThreadStart(n.RunScript));
t1.Start();
Thread t2 = new Thread(new ThreadStart(n.RunScript));
t2.Start();
....
}
....
}
class ClassTest
{
RunspacePool rsp;
public void intializeConnection(RunspacePool _rsp)
{
rsp = _rsp;
}
public void RunScript()
{
PowerShell ps = PowerShell.Create();
ps.RunspacePool = rsp;
ps.AddCommand("Get-VMMServer") AddParameter(...); // Doing this in every thread.
ps.AddCommand("Get-VM").AddParameter("Get-VM", "someVM");
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject result in ps.Invoke())
{
...
}
}
}
Get-VMMServer connects to a Virtual Machine Manager server (if a connection does not already exist) and retrieves the object that represents this server from the Virtual Machine Manager database.
I want this connection to be reused by each threads.
How can I achieve this? Is there a way to create this connection already in the Main() so that all the Runspaces in the pool can use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将与 VMM 服务器的连接设为单例,并让每个线程都使用该连接...
You could try making the connection to VMM Server a singleton and having every thread use that...