Top Banner
EVENT LOOPS Everything you wanted to know but couldn't find on StackOverflow
27

A Gentle Introduction to Event Loops

Jul 25, 2015

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: A Gentle Introduction to Event Loops

EVENT LOOPSEverything you wanted to know

but couldn't find on StackOverflow

Page 2: A Gentle Introduction to Event Loops

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

Page 3: A Gentle Introduction to Event Loops

WE ALL START SOMEWHERE

<?php echo "im in ur pagez, writin php!!1!" ?>

alert("Hi mom!");

Page 4: A Gentle Introduction to Event Loops

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")

Page 5: A Gentle Introduction to Event Loops

…BECAUSE NETWORKS

Page 6: A Gentle Introduction to Event Loops

FORKING

Page 7: A Gentle Introduction to Event Loops

<?php

function ping($host) { ... }

foreach ($hosts as $host) { $pid = pcntl_fork(); if ($pid) { pcntl_wait($status); } else { ping($host); } } ?>

Page 8: A Gentle Introduction to Event Loops

THREADING

Page 9: A Gentle Introduction to Event Loops

Some people, when confronted with a problem, think, “I know, I'll use threads”—

now two they hav erpoblesm.

Page 10: A Gentle Introduction to Event Loops

Knock knock.Race condition.Who's there?

Page 11: A Gentle Introduction to Event Loops

–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.”

Page 12: A Gentle Introduction to Event Loops

NON-BLOCKING IO

Page 13: A Gentle Introduction to Event Loops

LET’S ORDER BREAKFAST!

pancakes

soft-boiled egg

orange juice

Page 14: A Gentle Introduction to Event Loops

BLOCKING CHEF

mix pancakes heat skillet cook

pancakes boil water cook eggs cut juice

Page 15: A Gentle Introduction to Event Loops

NON-BLOCKING CHEF

mix pancakes

heat skillet cook pancakes

boil water cook eggs

cut juice

Page 16: A Gentle Introduction to Event Loops

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 ...

Page 17: A Gentle Introduction to Event Loops

EVENT LOOPS

Page 18: A Gentle Introduction to Event Loops
Page 19: A Gentle Introduction to Event Loops

while (!mExiting) NS_ProcessNextEvent(thread);

Page 20: A Gentle Introduction to Event Loops

DEVICES & GAMES

Page 21: A Gentle Introduction to Event Loops
Page 22: A Gentle Introduction to Event Loops

UNITY 3D GAME LOOP

• physics loop: runs until caught up to current frame

• event updates once per loop iteration ("tick")

• network events

• rendering

Page 23: A Gentle Introduction to Event Loops

$.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; });

Page 24: A Gentle Introduction to Event Loops

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)

Page 25: A Gentle Introduction to Event Loops

<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!") }

Page 26: A Gentle Introduction to Event Loops

CAVEAT EMPTOR

• event loops can be hard to work with

• don't use callbacks except for simple things

• use Promises, Futures and other abstractions

Page 27: A Gentle Introduction to Event Loops

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