簡體   English   中英

每秒放置一個全局HTTP請求的限制

[英]Placing a global HTTP requests per second limit

由於服務器的限制,我不能每3秒發出一個以上的請求,我正在使用Thread.Sleep()限制我可以發出的請求數量。 有沒有更好的方法而不必暫停線程? 謝謝。

static void main(string[] args)
{
    // getids
    List<string> requestIds = GetMyRequestIds();

    foreach(string requestId in requestIds)
    {
        Thread.Sleep(3000);

        // one request for each Id
        result = FetchStatus(requestId);
    }

}

public Dictionary<string, object> FetchStatus(string requestId)
{
    // build http request and query the server
    // ... requestId... http... etc... read to end
    return results;
}

如果您的限制是每3秒只有一個請求,則可以設置一個計時器,每3秒觸發一次回調。 由於這是在單獨的線程中執行的,因此可能會同時執行兩個長時間運行的請求。

System.Threading.Timer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM