task

How to run tasks concurrently in asyncio?

点点圈 提交于 2019-12-10 18:46:49
问题 I'm trying to learn how to run tasks concurrently using Python's asyncio module. In the following code, I've got a mock "web crawler" for an example. Basically, I am trying to make it where there are a max of two active fetch() requests happening at any given time, and I want process() to be called during the sleep() period. import asyncio class Crawler(): urlq = ['http://www.google.com', 'http://www.yahoo.com', 'http://www.cnn.com', 'http://www.gamespot.com', 'http://www.facebook.com', 'http

Does Task.WhenAll run Tasks in background thread parallel

倾然丶 夕夏残阳落幕 提交于 2019-12-10 17:53:47
问题 Are the following 2 code fragments do the same? //-------------------------------------------------- 1. //-------------------------------------------------- var producer = Task.Run(async () => { await bar.ReadDataAsync(); }); var consumer = Task.Run(async () => { await bar.WriteDataAsync(); }); await Task.WhenAll(consumer, producer); //-------------------------------------------------- 2. //-------------------------------------------------- await Task.WhenAll(bar.ReadDataAsync(), bar

Rake Default Task and Namespaces

梦想的初衷 提交于 2019-12-10 17:45:37
问题 I have read through the docs and looked at quite a few examples but I am not clear on defaults and namespaces. (using rake, version 10.0.3) First it seems, though I do not recall seeing this explicitly, that there can be only ONE default task regardless of how many are defined. Apparently the load order (PROJECT_NAME::Application.load_tasks) determines the winner. When I have struggled to create a namespaced default I have found that I have sometimes overridden the normal default for a rails

Download large files which takes more than 10 min when iPad device is locked

早过忘川 提交于 2019-12-10 17:36:10
问题 I have a requirement where user can download multiple files one after other. When my app goes in background OR when iPad is locked, the download or web-service response can be get only for 10 minutes. My download happens in a separate thread, I have implemented beginBackgroundTaskWithExpirationHandler: after some googling and on stackoverflow links. How to implement Task completion App crash because of auto lock in iphone? and iOS documetation https://developer.apple.com/library/ios/

UnobservedTaskException is not killing the process

[亡魂溺海] 提交于 2019-12-10 17:28:58
问题 I am trying to understand the UnobservedTaskException issue in .NET 4.0 so I wrote the folloging code TaskScheduler.UnobservedTaskException += (sender, eventArgs) => Console.WriteLine("unobserved"); Task.Factory.StartNew(() => { throw new Exception(); }, TaskCreationOptions.LongRunning); using (var autoResetEvent = new AutoResetEvent(false)) { autoResetEvent.WaitOne(TimeSpan.FromSeconds(10)); } Console.WriteLine("Collecting"); GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine(

Try/Catch Wrap Around Task.Run not Handling Exception

安稳与你 提交于 2019-12-10 17:17:30
问题 I've been learning to use TPL and have an issue with an example I gathered from this article. I copy and pasted the code exactly as in the Task.Run example but get an error message saying the exception isn't handled: private async void button1_Click(object sender, EventArgs e) { try { await Task.Run(() => { Thread.Sleep(1000); throw new InvalidOperationException("Hi!"); }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } Here's a picture of the error: Is this code example outdated

Does Task.ConfigureAwait(false) on the last method line affect anything?

社会主义新天地 提交于 2019-12-10 17:09:34
问题 I understand that calling ConfigureAwait(false) on a task that is being awaited will sometimes have a performance benefit, as it prevents an unnecessary return to the original SynchroniZationContext. For example: async Task Something() { // Let's say I'm on the UI context //... await AnotherTask.ConfigureAwait(false); // Code here is no longer running on the UI context. // It runs in a thread pool synchronization context (i.e. null). } My question is this: If the task call is on the last line

Getting the method name of a task

半腔热情 提交于 2019-12-10 16:56:23
问题 I am looking to get the method/action name from a task in C#. Specifically I am implementing a custom task scheduler, and would like to generate statistics on the duration a task runs, which I will then aggregate by the method running inside of the task. In the visual studio debugger you can access this and see the m_action private variable, as well as the debugger display annotation, displays it as Method={0}. Is there any way to get access to this from the Task itself? 回答1: Well, you could

Getting reference to original Task after ordering Tasks by completion?

南楼画角 提交于 2019-12-10 16:46:03
问题 I asked a question a while ago about a method that orders a List<Task<T>>> by their completion that also returns an int representing the index of the completed Task in the original List<Task<T>> given. I have the inkling that I might not need to return this int to determine which specific Task has completed and that I can interrogate the returned Task for this information. As a side note, I have since altered the method to order a List<Task> . I originally used Task<T> , which returned a bool

Why does await not wait?

那年仲夏 提交于 2019-12-10 16:13:13
问题 This is my actual code: async Task getData() { Thread.Sleep(5000); Console.WriteLine("Step 1"); using (HttpClient api = new HttpClient()) await api.GetAsync("http://google.com/").ContinueWith( (getTask) => Console.WriteLine(getTask.Result.StatusCode); ); Console.WriteLine("Step 2"); } private void button1_Click(object sender, EventArgs e) { Task<Task> task = new Task<Task>(getData); task.Start(); task.Wait(); Console.WriteLine("Step 3"); } I get the following output: Step 1 Step 3 OK Step 2