4

私はこれに数日間取り組んでおり、複数のスレッドと BLOB クライアントに関する以前の質問を読み、その提案を実装しました。

問題を以下に要約しました。

エラーは生成されず、threadtestコンテナー (既に存在する) には何も書き込まれません。1 つのブロブが書き込まれ、その後何も書き込まれないことがあります。

Sleep を 1 秒に増やすと、すべて問題ありません。

コードの理由は、Azure の BLOB 書き込み機能のベンチマークを行うことです。(私は現在、1 時間に 700,000 を実行する 8 つのシングルスレッド インスタンスを持っていますが、これを理解できれば、さらに高くできると確信しています)

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();
        }
    }
}
4

1 に答える 1

2

上記のコードでは、同時スレッドが多すぎて生成されました。スロットリングの私の単純なアプローチではThread.Sleep()、スレッド数を制限するのに十分ではありませんでした。

セマフォ (基本的には、同時に実行されているスレッドの数をカウントするメカニズム) を導入することで、問題は劇的に解決されました。同時実行制限とインスタンス数を着実に増やしており、すでに 1 時間あたり 100 万を超えています。(実際のコードは、16 ~ 32K のランダムな長さのデータを生成し、奇数のデータは ~4MB - 10 個の同時スレッドを持つ 4 つのインスタンス)

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();
        }
    }
}
于 2013-06-26T14:19:46.087 に答える