我正在编写一个 C# 程序来生成并通过 FTP 上传 50 万个文件。我想并行处理4个文件,因为机器有4个核心,文件生成需要更长的时间。是否可以将以下 Powershell 示例转换为 C#?或者是否有更好的框架,例如 C# 中的 Actor 框架(如 F# MailboxProcessor)?
Powershell 示例 http://blogs.msdn.com/b/powershell/archive/2011/04/04/scaling-and-queuing-powershell-background-jobs.aspx
$maxConcurrentJobs = 3;
# Read the input and queue it up
$jobInput = get-content .\input.txt
$queue = [System.Collections.Queue]::Synchronized( (New-Object System.Collections.Queue) )
foreach($item in $jobInput)
{
$queue.Enqueue($item)
}
# Function that pops input off the queue and starts a job with it
function RunJobFromQueue
{
if( $queue.Count -gt 0)
{
$j = Start-Job -ScriptBlock {param($x); Get-WinEvent -LogName $x} -ArgumentList $queue.Dequeue()
Register-ObjectEvent -InputObject $j -EventName StateChanged -Action { RunJobFromQueue; Unregister-Event $eventsubscriber.SourceIdentifier; Remove-Job $eventsubscriber.SourceIdentifier } | Out-Null
}
}
# Start up to the max number of concurrent jobs
# Each job will take care of running the rest
for( $i = 0; $i -lt $maxConcurrentJobs; $i++ )
{
RunJobFromQueue
}
Update:
与远程 FTP 服务器的连接可能很慢,因此我想限制 FTP 上传处理。
假设您使用 TPL 构建它,您可以设置ParallelOptions.MaxDegreesOfParallelism http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism.aspx无论你想要它是什么。
并行.For http://msdn.microsoft.com/en-us/library/dd781401.aspx代码示例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)