如何在 for 循环中多线程启动进程? [英] How do I multi-thread a start-process in a for loop?

查看:61
本文介绍了如何在 for 循环中多线程启动进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,希望你能帮我解决.我们在晚上 rar 一个包含大约 300 个文件的目录,当 rar 时它一次一个.我想做 5 次左右,而不是在已经处理的情况下加倍.这是我当前的脚本,我该如何制作才能一次执行 5 个?

$Files = Get-ChildItem V:\SQL_Server_BackupsForEach($Files 中的 $File){$fullname = $File.fullname$rar = "m[f] -ep -m4 -mt8 -ri12 D:\RAR\$File.rar $全名"start-process -File "C:\Program Files\WinRAR\rar.exe" -ArgumentList $rar -Wait}

现在我花了最后几个小时试图了解 Runspace、Run-Jobs、安装不同的多线程脚本,但我迷失了它并不好笑.我真的希望有一种简单的方法来运行它,而不必安装额外的模块或弄乱其他东西.因为我希望能够移动它而不必安装额外的东西来让它工作.提前致谢.

解决方案

Invoke-Parallel 脚本非常适合我

https://github.com/RamblingCookieMonster/Invoke-Parallel

<预><代码>."C:\_Batch_Jobs\Invoke-Parallel.ps1"$Files = Get-ChildItem V:\SQL_Server_Backups$文件 |调用并行 -ScriptBlock {$fullname = $_.fullname$rar = "m[f] -ep -m4 -mt8 -ri12 D:\RAR\$_.rar $全名"start-process -File "C:\Program Files\WinRAR\rar.exe" -ArgumentList $rar -Wait} -油门 10

I have this little problem I am hoping you can help me with. We rar a directory at night that has about 300 files, when raring it does it one at a time. I would like to do 5 or so with out doubling up on ones already processed. here is my current script, how do I make it so it can do 5 at a time?

$Files = Get-ChildItem V:\SQL_Server_Backups

ForEach ($File in $Files) 
{ 
    $fullname = $File.fullname
    $rar = "m[f] -ep -m4 -mt8 -ri12 D:\RAR\$File.rar $Fullname"
    start-process -File "C:\Program Files\WinRAR\rar.exe" -ArgumentList $rar -Wait 
} 

Now I have spent the last few hours trying to get my head around Runspace, Run-Jobs, installing different multi threaded scripts, but I am so lost its not funny. I am really hoping there is a simple way to run this, without having to install additional modules or messing around with other stuff. As I want to be able to move this with out having to install additional stuff to get it to work. Thank you in advance.

解决方案

Invoke-Parallel script worked perfect for me

https://github.com/RamblingCookieMonster/Invoke-Parallel

. "C:\_Batch_Jobs\Invoke-Parallel.ps1"
$Files = Get-ChildItem V:\SQL_Server_Backups

$Files | Invoke-Parallel -ScriptBlock {
    $fullname = $_.fullname
    $rar = "m[f] -ep -m4 -mt8 -ri12 D:\RAR\$_.rar $Fullname"
    start-process -File "C:\Program Files\WinRAR\rar.exe" -ArgumentList $rar -Wait
} -throttle 10

这篇关于如何在 for 循环中多线程启动进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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