]> Dogcows Code - chaz/talk-event-driven-programming-in-perl/commitdiff
add reactor graph and asyncawait slides
authorCharles McGarvey <cmcgarvey@bluehost.com>
Sun, 17 Jun 2018 20:55:09 +0000 (14:55 -0600)
committerCharles McGarvey <cmcgarvey@bluehost.com>
Sun, 17 Jun 2018 20:59:52 +0000 (14:59 -0600)
.gitignore
css/slides.css
img/eventloop.dot
img/reactor.dot [new file with mode: 0644]
js/slides.js
slides.html

index b0b0f730d1dc0771f07a3bce7cc384b5753574fb..2d60b80d83a6e22214614660809b498b6d3f6d42 100644 (file)
@@ -1,4 +1,5 @@
 *.pdf
 /img/eventloop.svg
+/img/reactor.svg
 /remark.min.js
 /slides-offline.html
index 36ce1a2e31944fb96698b4319805f3c4c97fba77..93fa17486d9b8bc8db50631d2fb76a3ad768a0c0 100644 (file)
@@ -3,6 +3,14 @@
     background: url('../img/bluehost.png') 1100px;
 }
 
+#slide-graph-eventloop img {
+    width: 100%;
+}
+#slide-graph-reactor img {
+    width: 100%;
+}
+
+
 .pizza img {
     width: 70%;
 }
@@ -41,3 +49,7 @@
     font-size: 38px;
 }
 
+.ex-asyncawait .perl {
+    font-size: 32px;
+}
+
index 3c0c3b3d9f79e129cee9d85b6bd106598de87062..e8a8cbe7c00467e2ca86551f9988b636e2e4344d 100644 (file)
@@ -22,7 +22,7 @@ digraph G {
 
     "Event source 1" -> "Wait for\nan event\nto happen"
     "Event source 2" -> "Wait for\nan event\nto happen"
-    "Event source n" -> "Wait for\nan event\nto happen"
+    "Event source N" -> "Wait for\nan event\nto happen"
 
     "Wait for\nan event\nto happen" -> "Handle an\nevent" [label="Something happened...\n", tailport="n", headport="n"]
     "Handle an\nevent" -> "Wait for\nan event\nto happen" [tailport="s", headport="s"]
diff --git a/img/reactor.dot b/img/reactor.dot
new file mode 100644 (file)
index 0000000..284b606
--- /dev/null
@@ -0,0 +1,37 @@
+
+digraph G {
+    rankdir = LR
+       
+    node
+    [
+        fontname    = "Inconsolata"
+        fontsize    = 20
+        shape       = record
+        style       = rounded
+        margin      = "0.2,0.2"
+    ]
+
+    edge
+    [
+        fontname    = "Inconsolata"
+        fontsize    = 18
+        arrowhead   = vee
+        arrowtail   = vee
+        arrowsize   = 2
+    ]
+
+    "Event source 1" -> "Wait for\nan event\nto happen"
+    "Event source 2" -> "Wait for\nan event\nto happen"
+    "Event source N" -> "Wait for\nan event\nto happen"
+
+    "Wait for\nan event\nto happen" -> "Demultiplex and\ndispatch events" [tailport="n", headport="n"]
+    "Demultiplex and\ndispatch events" -> "Wait for\nan event\nto happen" [tailport="s", headport="s"]
+
+    "Demultiplex and\ndispatch events" -> "Event handler 1"
+    "Demultiplex and\ndispatch events" -> "Event handler 2"
+    "Demultiplex and\ndispatch events" -> "Event handler M"
+
+
+    "Demultiplex and\ndispatch events" [style="rounded,filled",fillcolor="#FFFF88"]
+}
+
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..078d1949544b558c54bf8044fc0990c5eca54956 100644 (file)
@@ -0,0 +1 @@
+createHotkey(1, 'graph-eventloop');
index 9eae8008ce65f65a0a2751af948d93738c50c4ee..d86ee03aae1a18331b5ee606173fcdcf85011bb0 100644 (file)
@@ -54,6 +54,9 @@ my $pizza = prepare_and_bake($order, $ingredentials);
 print($pizza);
 ```
 
+???
+But some programs are long-lived.
+
 ---
 ## Event-driven programs
 
@@ -68,8 +71,11 @@ At it's core, event-driven programming is this simple.
 But there are some complications and things to knows, which is why this talk exists.
 
 ---
+name:  graph-eventloop
 class: center, middle
 
+## Event loop
+
 ![Event loop](img/eventloop.svg)
 
 ???
@@ -364,9 +370,30 @@ class: syscalls
 ### syscalls
 
 - [`pause`](http://man.he.net/man2/pause) - Sleeps until signal
+
+--
 - [`select`](http://man.he.net/man2/select), [`poll`](http://man.he.net/man2/poll), [`epoll`](http://man.he.net/man7/epoll), [`kqueue`](https://www.freebsd.org/cgi/man.cgi?format=ascii&sektion=2&query=kqueue) - Monitor multiple file descriptors
+
+--
 - [`clock_gettime`](http://man.he.net/man2/clock_gettime) - What time is it now?
 
+---
+
+## Reactor pattern
+
+.big[
+- Queues events asynchronously.
+- Demultiplexes and dispatches synchronously.
+]
+
+---
+name:  graph-reactor
+class: center, middle
+
+## Reactor pattern
+
+![Reactor](img/reactor.svg)
+
 ---
 class: ex-basicreactor
 
@@ -386,6 +413,25 @@ while (1) {
 }
 ```
 
+---
+class: ex-basicreactor
+
+## The basic reactor
+
+```perl
+our $timers     = [...];
+our $io_handles = [...];
+
+while (1) {
+    my $next_timer = find_next_timer($timers);
+
+*   poll($io_handles, $next_timer->time_from_now);
+
+    handle_ready_io_handles($io_handles);
+    handle_expired_timers($timers);
+}
+```
+
 ---
 
 ## Reactor examples on CPAN
@@ -399,6 +445,66 @@ while (1) {
 - [`Mojo::Reactor::Poll`](https://metacpan.org/source/Mojo::Reactor::Poll)
 ]
 
+---
+class: center, middle
+
+## Use [`Future::AsyncAwait`](https://metacpan.org/pod/Future::AsyncAwait).
+
+???
+If you have used JavaScript recently, you may have used its "async/await" feature to clean up your non-blocking code.
+
+---
+class: center, middle
+
+### Yes, Perl can do it, too!
+
+---
+class: ex-asyncawait
+
+## Without async and await
+
+```perl
+use Future;
+
+sub do_two_things {
+    return do_first_thing()->then(sub {
+        my $first = shift;
+
+        return do_second_thing($first)->then(sub {
+            my $second = shift;
+
+            return Future->done([$first, $second]);
+        });
+    });
+}
+```
+
+---
+class: ex-asyncawait
+
+## With async and await
+
+```perl
+use Future::AsyncAwait;
+
+async sub do_two_things
+{
+    my $first = await do_first_thing();
+
+    my $second = await do_second_thing($first);
+
+    return [$first, $second];
+}
+```
+
+???
+There are caveats: Localized variable assignments don't work, nor anything that has implied local-like behavior.
+---
+
+## Events in the world
+
+
+
 ---
 class: center, middle
 name:  conclusion
This page took 0.030691 seconds and 4 git commands to generate.