简体   繁体   English

每秒放置一个全局HTTP请求的限制

[英]Placing a global HTTP requests per second limit

due to server limitations, I cannot make more than one requests per 3 second, I am using Thread.Sleep() to limit the number of requests I can make. 由于服务器的限制,我不能每3秒发出一个以上的请求,我正在使用Thread.Sleep()限制我可以发出的请求数量。 Is there a better way without having to pause the thread? 有没有更好的方法而不必暂停线程? Thanks. 谢谢。

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;
}

If your limitation is just one request per 3 seconds you could set up a timer which fires a callback every 3 seconds. 如果您的限制是每3秒只有一个请求,则可以设置一个计时器,每3秒触发一次回调。 As this is executed in a separate thread, it is possible that two long running requests execute simultaneously. 由于这是在单独的线程中执行的,因此可能会同时执行两个长时间运行的请求。

System.Threading.Timer System.Threading.Timer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM