python - C# 相当于 Python 的 ThreadPool

标签 python c# multithreading threadpool

我想问一下Python的Threadpool函数是否有替代方案。 我是 C# 的新手。我有一个将字符串作为输入的函数,并且我有一个输入列表。我想要的是在多个线程(最多 12 个)中执行该函数,以便每个线程从列表中输入一个字符串,如果列表的大小大于线程数,则它等待任何一个线程完成其执行,然后在具有下一个输入的线程中再次启动该函数。希望这个问题对大家有意义。

我已经用 Python 完成了这个:

def myfunc(input):
 print(input)

if__name__ == "__main__":
 inputs = [1,12,2,3,4,5,6,7,8,9,1,0,1,2,3,4,5,6,4,8,97,7]
 ThreadPool(12).map(myfunc, inputs)

最佳答案

static void Main()
{
    var inputs = new[] { 1, 12, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0, 1, 2, 3, 4, 5, 6, 4, 8, 97, 7 };
    Parallel.ForEach(inputs, new ParallelOptions { MaxDegreeOfParallelism = 12 }, MyFunc);
}
static void MyFunc(int value) { /*...*/ }

关于python - C# 相当于 Python 的 ThreadPool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62282760/

相关文章:

c# - 使用简单委托(delegate)

c# - 使用 C# 客户端将文件上传到 https 网络服务器

.net - 做跨线程 Winforms 的最简单方法是什么?

python - 决定折叠这棵树的截止点的算法?

python - 如何根据列中的值合并列中的值

python - Django 应用程序尚未加载 Celery 任务

c# - 如何以编程方式获取最小化应用程序的屏幕截图?

multithreading - Rust 的 channel 如何实现?

缩放线程运行时的 C++ 并发问题

python - 如何从文本文件中获取多个坐标?