gpt4 book ai didi

multithreading - 解压缩适用于单线程,但不适用于多线程

转载 作者:行者123 更新时间:2023-12-03 00:50:41 25 4
gpt4 key购买 nike

我正在尝试使用PowerShell解压缩大量文件。我认为这是并行化的好地方。但是,我尝试并行化似乎使解压缩无效,即使它在单线程模式下也是如此。

$unzip = {
param([string]$sourceFile, [string]$destinationDir)
#Clean out the destination if it exists
rmdir $destination -Force -Recurse -ErrorAction SilentlyContinue
mkdir $destination -Force

#Actual unzip
$shell = new-object -com shell.application
$zipFile = $shell.NameSpace($sourceFile)
$destinationDir = $shell.NameSpace($destination)
$destinationDir.copyhere($zipFile.items())
}

foreach($file in $files){
$args = ($file.FullName, $destinationDir)
Start-Job $unzip -ArgumentList $args
}

#Cleanup
While (Get-Job -State "Running") { Start-Sleep 2 }
Remove-Job *

当我在没有多线程代码的情况下运行此程序时,它可以正常运行,但实际上没有文件解压缩。为什么是这样?

最佳答案

不知道您的示例是否复制粘贴,但是您的参数是$ destinationDir,但是您引用$ destination,然后使用$ destination创建$ destinationDir。我假设这是一个错字。我修复了您的功能,它可以按您期望的那样工作。

$unzip = {
param([string]$sourceFile, [string]$destination)
#Clean out the destination if it exists
rmdir $destination -Force -Recurse -ErrorAction SilentlyContinue
mkdir $destination -Force

#Actual unzip
$shell = new-object -com shell.application
$zipFile = $shell.NameSpace($sourceFile)
$destinationDir = $shell.NameSpace($destination)
$destinationDir.copyhere($zipFile.items())
}

使用receive-job会向您显示以下错误,指出正确的方向:
Cannot bind argument to parameter 'Path' because it is null.
+ CategoryInfo : InvalidData: (:) [mkdir], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,mkdir
+ PSComputerName : localhost

Method invocation failed because [System.String] doesn't contain a method named 'copyhere'.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
+ PSComputerName : localhost

我仍然建议如果可能的话,从shell.application的comobject转移到使用System.IO.Compression的.Net解决方案。还要注意,PowerShell作业的硬上限为5个同时运行的作业。我不确定这在v5中是否已解决。 CookieMonster使用基于 excellent post的运行空间编写了一个 functionwork by Boe Prox,这是一种更好的处理并发性能的方法。

关于multithreading - 解压缩适用于单线程,但不适用于多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28509659/

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