Commits

Yaron Minsky committed 1a968bf

added async example

  • Participants
  • Parent commits 996e5f2

Comments (0)

Files changed (9)

File workshop/async/MANIFEST

+_tags
+build.sh
+build_all.sh
+simple.ml
+time_loop.ml

File workshop/async/_tags

+true: syntax(camlp4o)
+true: package(core,sexplib.syntax,bin_prot.syntax,comparelib.syntax,fieldslib.syntax,variantslib.syntax,async,core_extended)
+true: thread,debug,annot

File workshop/async/build.sh

+#!/usr/bin/env bash
+
+eval `opam config -env`
+
+ocamlbuild -j 4 -use-ocamlfind -cflags "-w @A-4-33-23" -cflags -short-paths $*
+
+

File workshop/async/build_all.sh

+#!/usr/bin/env bash
+
+EXT=native
+
+./build.sh \
+    simple.$EXT \
+    time_loop.$EXT \
+    manifest.$EXT \
+    pipe_manifest.$EXT \
+

File workshop/async/manifest.ml

+open Core.Std
+open Async.Std
+
+let file_size filename =
+  Reader.file_contents filename >>| String.length
+
+let manifest_size manifest_filename =
+  Reader.file_lines manifest_filename
+  >>= fun manifest ->
+  Deferred.all (List.map ~f:file_size manifest)
+  >>= fun lengths ->
+  return (List.fold ~f:(+) ~init:0 lengths)
+;;
+
+let run () =
+  manifest_size "MANIFEST"
+  >>= fun manifest_size ->
+  printf "Total file size: %d\n" manifest_size;
+  shutdown 0;
+  return ()
+;;
+
+let () =
+  don't_wait_for (run ());
+  never_returns (Scheduler.go ());

File workshop/async/pipe_manifest.ml

+open Core.Std
+open Async.Std
+
+let file_size filename =
+  Reader.file_contents filename >>| String.length
+
+let manifest_size manifest_filename =
+  let read_file manifest_file =
+    let lines = Reader.lines manifest_file in
+    Pipe.fold' lines ~init:0 ~f:(fun sum lineq ->
+      Deferred.Queue.fold lineq ~init:sum ~f:(fun sum line ->
+        file_size line
+        >>= fun file_size ->
+        return (sum + file_size)))
+  in
+  Reader.with_file manifest_filename ~f:read_file
+;;
+
+let run () =
+  manifest_size "MANIFEST"
+  >>= fun manifest_size ->
+  printf "Total file size: %d\n" manifest_size;
+  shutdown 0;
+  return ()
+;;
+
+let () =
+  don't_wait_for (run ());
+  never_returns (Scheduler.go ());

File workshop/async/simple.ml

+open Core.Std
+open Async.Std
+
+let def =
+  after (sec 0.5)
+  >>= fun () ->
+  printf "Hello ";
+  after (sec 1.)
+  >>= fun () ->
+  printf "world!\n";
+  shutdown 0;
+  return ()
+
+let () =
+  never_returns (Scheduler.go ())

File workshop/async/time_loop.ml

+open Core.Std
+open Async.Std
+
+let rec print_loop s =
+  after (sec (Random.float 0.1))
+  >>= fun () ->
+  printf "[%s]" s;
+  print_loop s
+
+let () =
+  don't_wait_for (print_loop "a");
+  don't_wait_for (print_loop "b");
+  never_returns (Scheduler.go ())

File workshop/part_II.org

  - Synchronous reasoning
  - Ability to sequence operations
  - Still cooperative (so one thread can block the world)
-
-
+* A basic example