Browse Source

test8 - vibe async task

221V 2 weeks ago
parent
commit
2acb55df97
2 changed files with 47 additions and 1 deletions
  1. 10 1
      vtest2/source/app.d
  2. 37 0
      vtest2/source/test8.d

+ 10 - 1
vtest2/source/app.d

@@ -54,10 +54,18 @@ import std.concurrency : spawn;
 import test6 : test6_spawner;
 */
 
+/*
 // test7
 import std.concurrency : spawn;
 
 import test7 : test7_spawner;
+*/
+
+
+// test8
+import std.concurrency : spawn;
+
+import test8 : test8_spawner;
 
 
 void main(){
@@ -67,7 +75,8 @@ void main(){
   //spawn(&test4_spawner); // test4 run
   //spawn(&test5_spawner); // test5 run
   //spawn(&test6_spawner); // test6 run
-  spawn(&test7_spawner); // test7 run
+  //spawn(&test7_spawner); // test7 run
+  spawn(&test8_spawner); // test8 run
   
   //Thread.sleep(dur!"seconds"(125));
   //Thread.sleep(dur!"seconds"(3));

+ 37 - 0
vtest2/source/test8.d

@@ -0,0 +1,37 @@
+
+
+import std.stdio;
+
+import core.time : Duration, dur;
+
+import vibe.core.core;
+import vibe.core.concurrency;
+
+
+void test8_spawner(){
+  auto val = async({
+    //logInfo("Starting to compute value in worker task.");
+    writeln("Starting to compute value in worker task.");
+    sleep(dur!"msecs"(500));
+    writeln("Finished computing value in worker task.");
+    return 32;
+  });
+  
+  writeln("Starting computation in main task");
+  sleep(dur!"msecs"(200));
+  writeln("Finished computation in main task. Waiting for async value.");
+  writeln("Result: ", val.getResult());
+}
+
+/*
+
+./vtest2
+hello here!
+Starting to compute value in worker task.
+Starting computation in main task
+Finished computation in main task. Waiting for async value.
+Finished computing value in worker task.
+Result: 32
+
+*/
+