サーバーからapiデータを取得する非同期メソッドがあります。このコードをローカルマシン上でコンソールアプリケーションで実行すると、高速で実行され、1分あたりのasync関数で数百回のhttpコールが実行されます。しかしAzure WebJobのキューメッセージから同じコードを起動すると、同期して動作しているように見えますが、私の数値クロールは簡単です。Azure WebJob関数で非同期を行う方法
キュー上のメッセージをリッスンし、メッセージに対する処理は、受信取得APIをキックオフ(1).. WebJob関数:
public class Functions
{
// This function will get triggered/executed when a new message is written
// on an Azure Queue called queue.
public static async Task ProcessQueueMessage ([QueueTrigger("myqueue")] string message, TextWriter log)
{
var getAPIData = new GetData();
getAPIData.DoIt(message).Wait();
log.WriteLine("*** done: " + message);
}
}
外部アズール速度で非同期モードで動作すること(2)クラスあなたが期待すべき行動は、あなたのプロセスは、一度に1つのメッセージを実行し、処理することで、より多くのインスタンスが作成された場合はスケールアップします(アプリサービスの - ...
class GetData
{
// wrapper that is called by the message function trigger
public async Task DoIt(string MessageFile)
{
await CallAPI(MessageFile);
}
public async Task<string> CallAPI(string MessageFile)
{
/// create a list of sample APIs to call...
var apiCallList = new List<string>();
apiCallList.Add("localhost/?q=1");
apiCallList.Add("localhost/?q=2");
apiCallList.Add("localhost/?q=3");
apiCallList.Add("localhost/?q=4");
apiCallList.Add("localhost/?q=5");
// setup httpclient
HttpClient client =
new HttpClient() { MaxResponseContentBufferSize = 10000000 };
var timeout = new TimeSpan(0, 5, 0); // 5 min timeout
client.Timeout = timeout;
// create a list of http api get Task...
IEnumerable<Task<string>> allResults = apiCallList.Select(str => ProcessURLPageAsync(str, client));
// wait for them all to complete, then move on...
await Task.WhenAll(allResults);
return allResults.ToString();
}
async Task<string> ProcessURLPageAsync(string APIAddressString, HttpClient client)
{
string page = "";
HttpResponseMessage resX;
try
{
// set the address to call
Uri URL = new Uri(APIAddressString);
// execute the call
resX = await client.GetAsync(URL);
page = await resX.Content.ReadAsStringAsync();
string rslt = page;
// do something with the api response data
}
catch (Exception ex)
{
// log error
}
return page;
}
}
「getAPIData.DoIt(メッセージ).Wait();」を変更しようとしましたか? "getAPIData.DoIt(message);を待つ"; –
ありがとうございましたJason - あなたと他の人のコメントが私の仕事を助けました。とても有難い。 – qtime67