相关疑难解决方法(0)

如何限制并发异步I/O操作的数量?

// let's say there is a list of 1000+ URLs
string[] urls = { "http://google.com", "http://yahoo.com", ... };

// now let's send HTTP requests to each of these URLs in parallel
urls.AsParallel().ForAll(async (url) => {
    var client = new HttpClient();
    var html = await client.GetStringAsync(url);
});
Run Code Online (Sandbox Code Playgroud)

这是问题所在,它会同时启动1000多个Web请求.有没有一种简单的方法来限制这些异步http请求的并发数量?这样在任何给定时间都不会下载超过20个网页.如何以最有效的方式做到这一点?

c# asynchronous task-parallel-library async-await async-ctp

103
推荐指数
5
解决办法
4万
查看次数

对HttpClient请求进行速率限制的简单方法

我在System.Net.Http中使用HTTPClient来对API发出请求.API限制为每秒10个请求.

我的代码大致是这样的:

    List<Task> tasks = new List<Task>();
    items..Select(i => tasks.Add(ProcessItem(i));

    try
    {
        await Task.WhenAll(taskList.ToArray());
    }
    catch (Exception ex)
    {
    }
Run Code Online (Sandbox Code Playgroud)

ProcessItem方法做了一些事情,但总是使用以下方法调用API : await SendRequestAsync(..blah). 看起来像:

private async Task<Response> SendRequestAsync(HttpRequestMessage request, CancellationToken token)
{    
    token.ThrowIfCancellationRequested();
    var response = await HttpClient
        .SendAsync(request: request, cancellationToken: token).ConfigureAwait(continueOnCapturedContext: false);

    token.ThrowIfCancellationRequested();
    return await Response.BuildResponse(response);
}
Run Code Online (Sandbox Code Playgroud)

最初代码工作正常,但是当我开始使用Task.WhenAll时,我开始从API获得"超出速率限制"消息.如何限制请求的速率?

值得注意的是,ProcessItem可以根据项目进行1-4次API调用.

c# async-await

9
推荐指数
3
解决办法
6996
查看次数

数据库中的C#多个并行插入

我有一个大约3000行的数据表.每个行都需要插入数据库表中.目前,我正在运行foreach循环,如下所示:

obj_AseCommand.CommandText = sql_proc;
obj_AseCommand.CommandType = CommandType.StoredProcedure;
obj_AseCommand.Connection = db_Conn;
obj_AseCommand.Connection.Open();

foreach (DataRow dr in dt.Rows)                
{
    obj_AseCommand.Parameters.AddWithValue("@a", dr["a"]);
    obj_AseCommand.Parameters.AddWithValue("@b", dr["b"]);
    obj_AseCommand.Parameters.AddWithValue("@c", dr["c"]);

    obj_AseCommand.ExecuteNonQuery();
    obj_AseCommand.Parameters.Clear();
}

obj_AseCommand.Connection.Close();
Run Code Online (Sandbox Code Playgroud)

您能否告诉我如何在数据库中并行执行SP,因为上述方法需要大约10分钟才能插入3000行.

c# ado.net multithreading sqlcommand

5
推荐指数
2
解决办法
2万
查看次数

如何将此 Parallel.ForEach 代码转换为 async/await

我在理解 async/await 时遇到了一些麻烦。我正在帮助使用具有以下代码的现有代码库(为简洁起见,已简化):

List<BuyerContext> buyerContexts = GetBuyers();
var results = new List<Result>();

Parallel.ForEach(buyerContexts, buyerContext =>
{
    //The following call creates a connection to a remote web server that 
    //can take up to 15 seconds to respond
    var result = Bid(buyerContext);

    if (result != null)
        results.Add(result);
}

foreach (var result in results)
{
  // do some work here that is predicated on the 
  // Parallel.ForEach having completed all of its calls
}
Run Code Online (Sandbox Code Playgroud)

如何使用 async/await 将此代码转换为异步代码而不是并行代码?我遇到了一些非常严重的性能问题,我认为这是对多个网络 I/O 操作使用并行方法的结果。

我自己尝试了几种方法,但我从 Visual Studio 收到警告,说我的代码将同步执行,或者我不能在异步方法之外使用 …

c# asynchronous

5
推荐指数
1
解决办法
1648
查看次数

C#Task.WaitAll没有等待

我的目标是从Amazon Web Services存储桶下载图像.

我有以下代码功能,可以一次下载多个图像:

public static void DownloadFilesFromAWS(string bucketName, List<string> imageNames)
{
    int batchSize = 50;
    int maxDownloadMilliseconds = 10000;

    List<Task> tasks = new List<Task>();

    for (int i = 0; i < imageNames.Count; i++)
    {
        string imageName = imageNames[i];
        Task task = Task.Run(() => GetFile(bucketName, imageName));
        tasks.Add(task);
        if (tasks.Count > 0 && tasks.Count % batchSize == 0)
        {
            Task.WaitAll(tasks.ToArray(), maxDownloadMilliseconds);//wait to download
            tasks.Clear();
        }
    }

    //if there are any left, wait for them
    Task.WaitAll(tasks.ToArray(), maxDownloadMilliseconds);
}

private static void GetFile(string …
Run Code Online (Sandbox Code Playgroud)

c# task

3
推荐指数
1
解决办法
801
查看次数