问题描述
我正在编写一个 C# 程序来通过 FTP 生成和上传五十万个文件.我想并行处理 4 个文件,因为机器有 4 个内核,文件生成需要更长的时间.是否可以将以下 Powershell 示例转换为 C#?或者有没有更好的框架比如C#中的Actor框架(比如F#MailboxProcessor)?
I am writing a C# program to generate and upload a half million files via FTP. I want to process 4 files in parallel since the machine have 4 cores and the file generating takes much longer time. Is it possible to convert the following Powershell example to C#? Or is there any better framework such as Actor framework in C# (like F# MailboxProcessor)?
$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
}
更新:
到远程 FTP 服务器的连接可能很慢,所以我想限制 FTP 上传处理.
Update:
The connection to remote FTP server can be slow so I want to limit the FTP uploading processing.
推荐答案
假设您使用 TPL 构建此方案,您可以设置 ParallelOptions.MaxDegreesOfParallelism 到任何你想要的.
Assuming you're building this with the TPL, you can set the ParallelOptions.MaxDegreesOfParallelism to whatever you want it to be.
Parallel.For 代码示例.
这篇关于限制 C# 中的并行线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!