This is a talk given at Clojure Conj 2014 in Washington DC. The title was Paradigms of core.async and it was given by Julian Gamble.
The code and demos are available here: https://github.com/juliangamble/clojure-conj-2014-paradigms-of-core-async
6. Core.async is about:
• queues in your application
• making your application simpler to reason
about
7. Benefits
In this talk you will gain an understanding of:
• go blocks and how they do concurrency
• core.async queues
8. Benefits
In this talk you will gain an understanding of:
• go blocks and how they do concurrency
• core.async queues
• go block timers
• alts! macro and how it enables reading multiple queues
9. Benefits
In this talk you will gain an understanding of:
• go blocks and how they do concurrency
• core.async queues
• go block timers
• alts! functions and how it enables reading multiple queues
• how core.async processes work in ClojureScript
• the visual impacts of how the core.async function enables
simple ‘process-level’ pauses
• how to use core.async to separate out the calculation and
display parts of your code
10. Benefits
In this talk you will gain an understanding of:
• converting a Clojure code-base to ClojureScript
• some tips for optimising ClojureScript
12. What is core.async?
• A set of primitives for creating, reading and
writing to queues
• A code walking macro that splices go blocks
into state machines
13. What is core.async?
• A set of primitives for creating, reading and
writing to queues
• A code walking macro that splices go blocks
into state machines
• A mechanism for asynchronous computation
• A library in both Clojure and ClojureScript
14. What does core.async say about
Clojure?
• core.async is implemented using a sophisticated Lisp
macro - something only possible in a functional
language
• in non-functional languages like Go and C# - CSP is
achieved by compiler-extensions - whereas in a
functional language like Clojure - this functionality
comes as a mere library
15. What is CSP?
• Communicating Sequential Processes
• Comes out of process calculi – an attempt in the
1960’s and 1970’s to optimise computer usage
through specialised algebra.
16. What is CSP?
• Communicating Sequential Processes
• Comes out of process calculi – an attempt in the
1960’s and 1970’s to optimise computer usage
through specialised algebra.
• Based on message passing via
channels
• Subject of 1978 Book by C.A.R. Hoare
33. Tim Baldridge 10K
processes
make-‐scene
function
make-‐cell
function
10K
go
block
1. Set
colour
2. Paint
canvas
cell
3. Pause
for
random
interval
4. Loop
45. swanodette 10K processes
[go
block]
let
block
[idx
v] [idx
v]
[go
block]
render-‐loop ‘render’ ‘queue’ render!
core.async
channel parameter
passed
in
during
function
call
46. swanodette 10K processes
[go
block]
let
block
[idx
v] [idx
v]
[go
block]
render-‐loop ‘render’ ‘queue’ render!
core.async
channel parameter
passed
in
during
function
call
No
more
than
1024
pending
puts
are
allowed
on
a
single
channel.
57. Parallelism is not
Concurrency
Imagine streams of execution in your program
58. Parallelism is not
Concurrency
Imagine streams of execution in your program
59. Parallelism is not
Concurrency
Imagine streams of execution in your program
60. Parallelism
1. For core.async "use it where ever you'd use a queue"
2. In the Tim Baldridge 10K processes example above - it is not explicitly using a
queue
(<! (timeout (rand-int 1000)))))))
61. Parallelism
1. For core.async "use it where ever you'd use a queue"
2. In the Tim Baldridge 10K processes example above - it is not explicitly using a
queue
(<! (timeout (rand-int 1000)))))))
3. But in the same example - it is implicitly using the queue of the core.async
process scheduler - so you're still using a queue.
4. Go blocks are the lightweight equivalent of 'heavyweight' threads
62. Parallelism
1. For core.async "use it where ever you'd use a queue"
2. In the Tim Baldridge 10K processes example above - it is not explicitly using a
queue
(<! (timeout (rand-int 1000)))))))
3. But in the same example - it is implicitly using the queue of the core.async
process scheduler - so you're still using a queue.
4. Go blocks are the lightweight equivalent of 'heavyweight' threads
5. Heavyweight threads implicitly listen to the queue of the OS 'ready queue'
6. You're already implicitly using queues whenever you use threads.
64. Quick recap
• core.async is all about queues
• queues can used in your application for:
• parallelism
• separating the calculation from the display
logic
• event handling
67. Rich Hickey original ants
demo
-‐main
function
send-‐off send-‐off send-‐off
animation
agent
ant
behaviour
agent
evaporation
agent
world
symbol
(vector
of
vectors)
73. Converting to ClojureScript
Missing functions in ClojureScript:
• defstruct - replace with a map
• alter - replace with swap!
74. Converting to ClojureScript
Missing functions in ClojureScript:
• defstruct - replace with a map
• alter - replace with swap!
• sync
• dosync
• agent
75. ants cljs no async
animate
function
requestAnimationFrame
single
call single
call single
send-‐off-‐
animation
function
behave-‐ants
function
(callback)
evaporate
function
world
symbol
(vector
of
vectors)
call
84. Using optimisations from David
Nolen’s chambered example
• Macros for creating arrays and for-loops
• Underlying assumptions:
• cljs data structures are not yet fully performant -
so consider replacing with arrays for speed
• The cljs compiler does not yet fully optimise
higher order functions - the most efficient
looping construct will be a for-loop macro that
uses the loop function
89. Back to The Italian Job
• Who or what was the hero of the story?
90. Summary
• core.async is about using queues
• core.async is about making your application simpler to reason about
91. Summary
• core.async is about using queues
• core.async is about making your application simpler to reason about
• classic applications are: user interface events, presentation loops,
parallelism in non-parallel environments
• you can consider a multithreaded application a queue listener even if
it doesn’t appear to use queues
92. Summary
• core.async is about using queues
• core.async is about making your application simpler to reason about
• classic applications are: user interface events, presentation loops,
parallelism in non-parallel environments
• you can consider a multithreaded application a queue listener even if
it doesn’t appear to use queues
• in porting an application from Clojure to ClojureScript you have to
rethink your concurrency
• There are lots of optimisations available to make ClojureScript
applications run faster
• core.async is not a magic sauce you can sprinkle everywhere - know
when to use it
93. Questions?
Clojure
Conj
Nov
2014
Julian
Gamble
@juliansgamble
github.com/juliangamble/clojure-‐conj-‐2014-‐paradigms-‐of-‐core-‐async