JavaScript Event Loop Not yo mama’s multithreaded approach. slidesha.re/ZPC2nD.

Slides:



Advertisements
Similar presentations
Facts about Welcome to this video from Ozeki. In this video I will present what makes Ozeki Phone System XE the Worlds best on-site software PBX for Windows.
Advertisements

N ODE.J S S ERVER S IDE J AVASCRIPT Diana Roiswati ( ) Ahmad Syafii ( ) Asri Taraqiadiyu ( )
COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
Tuple Spaces and JavaSpaces CS 614 Bill McCloskey.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
 A JavaScript runtime environment running Google Chrome’s V8 engine ◦ a.k.a. a server-side solution for JS ◦ Compiles JS, making it really fast  Runs.
INFM 603: Information Technology and Organizational Context Jimmy Lin The iSchool University of Maryland Thursday, October 18, 2012 Session 7: PHP.
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
Multithreaded Web Server.
Paradigms & Benchmarks Ryan McCune CSE Final Presentation 11/3/11 Notre Dame Computer Science 1.
Nachos Phase 1 Code -Hints and Comments
Node.js - What is Node.js? -
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Threads and Processes.
Concurrency: Threads, Address Spaces, and Processes Andy Wang Operating Systems COP 4610 / CGS 5765.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Concurrency Patterns Emery Berger and Mark Corner University.
Where does PHP code get executed?. Where does JavaScript get executed?
Concurrent Programming and Threads Threads Blocking a User Interface.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Debugging Threaded Applications By Andrew Binstock CMPS Parallel.
1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication.
Programming Paradigms By Tyler Smith. Event Driven Event driven paradigm means that the program executes code in reaction to events. The limitation of.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
Node.Js 1. 2 Contents About Node.Js Web requirement latest trends Introduction Simple web server creation in Node.Js Dynamic Web pages Dynamic web page.
CS533 - Concepts of Operating Systems 1 Threads, Events, and Reactive Objects - Alan West.
Code Development for High Performance Servers Topics Multithreaded Servers Event Driven Servers Example - Game Server code (Quake) A parallelization exercise.
LECTURE 11 Networking Part 2. NETWORKING IN PYTHON At this point, we know how to write a simple TCP client and simple TCP server using Python’s raw sockets.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
Introduction to Node.js® Jitendra Kumar Patel Saturday, January 31, 2015.
Threads by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
Tutorial 2: Homework 1 and Project 1
Lean With MEAN.
Introduction to Operating Systems
Node.Js Server Side Javascript
The Server-side JavaScript
BIT116: Scripting Lecture 06
Multi Threading.
CSE 775 – Distributed Objects Submitted by: Arpit Kothari
Processes and Threads Processes and their scheduling
Async or Parallel? No they aren’t the same thing!
Unconventional applications of Intel® Xeon Phi™ Processor (KNL)
Node.js talking to PROGRESS
Node.Js Server Side Javascript
Operating Systems (CS 340 D)
Parallel Programming in Contemporary Programming Languages (Part 2)
Introduction to Operating Systems
Android Programming Lecture 8
CSE 154 Lecture 22: AJAX.
03 | Building a Backend with Socket.IO and Mongo
Nathan Totten Technical Evangelist Windows Azure
Processes and Threads.
Lecture Topics: 11/1 General Operating System Concepts Processes
Background and Motivation
Due Next Monday Assignment 1 Start early
INTRODUCTION TO By Stepan Vardanyan.
Operating Systems (CS 340 D)
PROCESSES & THREADS ADINA-CLAUDIA STOICA.
Concurrency: Processes CSE 333 Summer 2018
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
Process Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
Introduction.
EECE.4810/EECE.5730 Operating Systems
Concurrency: Threads, Address Spaces, and Processes
Presentation transcript:

JavaScript Event Loop Not yo mama’s multithreaded approach. slidesha.re/ZPC2nD

Who is Thomas Hunter? Web developer for 8+ years Dow, Ford, Quicken, Startup, Barracuda Started development with PHP & MySQL Became a Node.js fanboy Writing a book on Backbone.js for | Web developer for 8+ years Dow, Ford, Quicken, Startup, Barracuda Started development with PHP & MySQL Became a Node.js fanboy Writing a book on Backbone.js for |

What does MultiThreaded mean? Makes use of separate CPU Cores as “Threads” Uses a single process within the Operating System True concurrency Can have Race Conditions (simultaneous memory use) “Hard” to get it right Ran out of Ghz, hardware adds more cores

JavaScript is SingleThreaded Makes use of a single CPU core CPU intensive work is never “concurrent” Easier to pull off, as in less technical difficulties

Technical Implementation Stack: Functions to run and available variables More added as code is run Stuff guaranteed to run in order Heap: “Chaotic” listing of objects Queue: Gets added to stack when stack empty setTimeout and setInterval added here Credit: Mozilla Developer Network

Example Code-run console.log("Adding code to the queue"); setTimeout(function() { // Added somewhere in Heap console.log("Running next code from queue"); }, 0); function a(x) { // Added somewhere in Heap console.log("a() frame added to stack"); b(x); console.log("a() frame removed from stack"); } function b(y) { // Added somewhere in Heap console.log("b() frame added to stack"); console.log("Value passed in is " + y); console.log("b() frame removed from stack"); } console.log("Starting work for this stack"); a(42); console.log("Ending work for this stack"); Adding code to the queue Starting work for this stacka() frame added to stackb() frame added to stackValue passed in is 42b() frame removed from stacka() frame removed from stackEnding work for this stackRunning next code from queue

Your App is Mostly Asleep Node.js: All I/O is non-blocking E.g. it gets thrown into the Queue Browser: Wait for a click to happen PHP: Wait for a MySQL query to run These show how slow I/O can be → L1-Cache 3 cyclesL2-Cache 14 cyclesRAM 250 cyclesDisk 41,000,000 Network 240,000,000

Sequential vs Parallel Traditional web apps perform each I/O Sequentially With an Event Loop, they can be run in Parallel Since most time is wasted doing I/O, very inefficient Traditional web apps perform each I/O Sequentially With an Event Loop, they can be run in Parallel Since most time is wasted doing I/O, very inefficient

Non JS Event Loops Event Loops can be implemented in other languages However, JavaScript was built this way, feels “natural” Examples: Ruby: EventMachine Python: Twisted & Tornado PHP: ReactPHP

!JS Event Loop Examples require 'eventmachine' module Echo def post_init puts "Someone Connected" end def receive_data data send_data "#{data}" close_connection if data =~ /quit/if end EventMachine.run { EventMachine.start_server " ", 8081, Echo } var net = require('net'); var server = net.createServer(function (socket) { console.log('Someone Connected'); socket.pipe(socket); }); server.listen(1337, ' '); EventMachine in RubyNode.js

Event Loops are Awesome! No race conditions Typical web apps spend their time waiting on I/O No funky syntax; it just works Perform I/O operations “in parallel” easily Stateful web applications are easy compared to PHP Long run apps, don’t need web servers, shared data...

Event Loops aren’t Awesome! CPU intensive work will block your process You can offload work to different processes (Node.js) It isn’t making use of those 8 cores you’ve got You can use the Multi-node module though (Node.js) Memory leaks are possible, not so with PHP You can program better and prevent it though ;-)

Web Workers You can use MultiThreaded JavaScript in your browser IE10, Firefox 3.5, Chrome 4, Safari 4, Opera 10.6 var worker = new Worker('task.js'); Use Message Passing to share data You share simple JSON data, not complex objects Prevents deadlocks/race conditions because of this

Conclusion Great for I/O bound applications (most web apps) Horrible for CPU bound applications (do it in C ;) Appears to be a single multi-threaded process “Fakes” concurrency slidesha.re/ZPC2nD thomashunter.name slidesha.re/ZPC2nD