c# - 任务线程和 Azure CloudBlobClient

标签 c# azure azure-blob-storage

我已经为此工作了几天,并阅读了之前有关多线程和 blob 客户端的问题并实现了他们的建议。

我已将问题归结为以下内容。

不会生成任何错误,只是没有将任何内容写入threadtest容器(已存在)。有时会写入一个 blob,然后什么也没有写入。

如果我将 sleep 时间增加到 1 秒,一切都很好。

编写该代码的原因是为了对 Azure 的 blob 写入功能进行基准测试。 (我目前有 8 个单线程实例,每小时执行 700,000 次,但我确信如果我能弄清楚这一点,我可以得到更高的值)

using System;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using System.Threading.Tasks;

namespace ThreadedWriterTest
{
    public class WorkerRole : RoleEntryPoint
    {
        private static CloudStorageAccount storageAccount;

        public override void Run()
        {
            while (true)
            {
                Thread.Sleep(10);
                Task.Factory.StartNew(()=> writeStuff());
            }
        }

        private void writeStuff()
        {
            CloudBlobClient threadClient = storageAccount.CreateCloudBlobClient();
            threadClient.GetBlobReference("threadtest/" + Guid.NewGuid().ToString()).UploadText("Hello " + Guid.NewGuid().ToString());
        }



        public override bool OnStart()
        {
            ServicePointManager.DefaultConnectionLimit = 12;
            storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("XXX"));
            return base.OnStart();
        }
    }
}

最佳答案

上面的代码生成了太多并发线程,我使用 Thread.Sleep() 进行节流的天真方法不足以限制线程数。

引入信号量(基本上是一种计算并发执行线程数的机制)极大地解决了这个问题。我正在稳步增加并发限制和实例数量,每小时已经超过 100 万。 (实际代码生成随机长度数据16-32K,奇数~4MB - 4个实例,10个并发线程)

using System;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using System.Threading.Tasks;

namespace ThreadedWriterTest
{
    public class WorkerRole : RoleEntryPoint
    {
        private static CloudStorageAccount storageAccount;
        private static Semaphore semaphore = new Semaphore(3, 3);

        public override void Run()
        {
            while (true)
            {
                semaphore.WaitOne();
                Task.Factory.StartNew(()=> writeStuff());
            }
        }

        private void writeStuff()
        {
            CloudBlobClient threadClient = storageAccount.CreateCloudBlobClient();
            threadClient.GetBlobReference("threadtest/" + Guid.NewGuid().ToString()).UploadText("Hello " + Guid.NewGuid().ToString());
            semaphore.Release();
        }



        public override bool OnStart()
        {
            ServicePointManager.DefaultConnectionLimit = 12;
            storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("XXX"));
            return base.OnStart();
        }
    }
}

关于c# - 任务线程和 Azure CloudBlobClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17297861/

相关文章:

c# - 寻找集体智慧 .Net/C# 资源

c# - 通过一次调用调用一组接口(interface)的方法

azure - 使用 Azure Functions 中的 Apollo Server 连接到多个数据库

SQL Azure - 在数据库之间复制表

azure - 降级 Service Fabric 应用程序版本

c# - 为什么任务并行不能加快上传/下载速度?

java - 代理后面的 Azure Blob 存储 sdk v10/v11 快速启动错误

c# - 通过 Entity Framework 提高 SQL Server 中的搜索性能

azure - Blob 触发器影响 Azure 函数中的应用程序洞察日志记录

c# - 对于 C# 日志记录,如何以最小的开销获取调用堆栈深度?