EVENT LOOPS Everything you wanted to know but couldn't find on StackOverflow
EVENT LOOPSEverything you wanted to know
but couldn't find on StackOverflow
ABOUT ME
• I've misunderstood event loops for over a decade
• 2–3 years ago I set out on a quest to really understand them
• I failed
• but learned a few things
WE ALL START SOMEWHERE
<?php echo "im in ur pagez, writin php!!1!" ?>
alert("Hi mom!");
WHY IS THIS SO SLOW?…for my $host (@hosts) { say $host . " is " . (ping($host) ? "up" : "down"); }
for host in hosts: print host + " is " + ("up" if ping(host) else "down")
…BECAUSE NETWORKS
FORKING
<?php
function ping($host) { ... }
foreach ($hosts as $host) { $pid = pcntl_fork(); if ($pid) { pcntl_wait($status); } else { ping($host); } } ?>
THREADING
Some people, when confronted with a problem, think, “I know, I'll use threads”—
now two they hav erpoblesm.
Knock knock.Race condition.Who's there?
–Apple, "Threaded Programming Guide: Thread Management"
“Another cost to consider when writing threaded code is the production costs. Designing a threaded application can sometimes require fundamental changes to the way you organize your application’s data structures. Making those changes might be necessary to avoid the use of synchronization, which can itself impose a tremendous performance penalty on poorly designed applications. Designing those data structures, and debugging problems in threaded code, can increase the time it takes to develop a threaded application. Avoiding those costs can create bigger problems at runtime, however, if your threads spend too much time waiting on locks or doing nothing.”
NON-BLOCKING IO
LET’S ORDER BREAKFAST!
pancakes
soft-boiled egg
orange juice
BLOCKING CHEF
mix pancakes heat skillet cook
pancakes boil water cook eggs cut juice
NON-BLOCKING CHEF
mix pancakes
heat skillet cook pancakes
boil water cook eggs
cut juice
for host in hosts: clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) clientsocket.setblocking(0) clientsocket.connect((host, port))
while 1: reads, writes, fails = select.select(in, out, [], wait) # do something with these handles ...
EVENT LOOPS
while (!mExiting) NS_ProcessNextEvent(thread);
DEVICES & GAMES
UNITY 3D GAME LOOP
• physics loop: runs until caught up to current frame
• event updates once per loop iteration ("tick")
• network events
• rendering
$.get("some/page.html", function (data) { $("#response").html(data); });
var req = http.request({path: 'some/page.html'}, function(res) { res.on('data', function(data) { console.log(data); }); });
$ua->get('some/page.html', sub { say pop->res->body; });
http.get(url, function(response) { print response.body})
Loop.Run()
reactor (event sources)
http.get(url function(response) { print response.body}
Loop.Run()
message queue (callbacks)
<script type=“text/javascript">
$(document).ready(function() { jQuery.ajax(“http://www.perl.org”, { success: function(data) { alert("loaded!") } }) })
</script>
jQuery.ajax(“http://www.perl.org”, { success: function(data) { alert("loaded!") } })
success: function(data) { alert("loaded!") }
CAVEAT EMPTOR
• event loops can be hard to work with
• don't use callbacks except for simple things
• use Promises, Futures and other abstractions
IMAGE CREDITS
• stopwatch, cloud, database, info by Austin Condiff from the Noun Project
• server by aLf from the Noun Project
• browser by Cindy Hu from the Noun Project
• code by useiconic.com from the Noun Project