- A super quick overview of core.async internals
- I was looking for different approaches for concurrency and parallelism in Clojure
- This talk does not cover every details about core.async (not even close)
- What is core.async ?
- How does it works ?
- Channels
- Go Blocks
- A Clojure/Clojurescript library for async programming.
- An implementation of CSP in Clojure/Clojurescript.
- Channels
- Channel operations
- Buffers
- `thread` macro
- `go` macro
- Threadpools
- It's like Bullet Two
- It's similar to `future`
- Creates an actual thread
- Returns a channel yielding the result
- Use it for IO operations
- It's a lightweight thread
- Returns a channel yielding the result or the block
- Do not use it for IO operations
- Under the hood it's really complicated.
- Execution threadpool
- Executes `go` blocks
- newFixedThreadPool
- `thread` threadpool
- Is the only implementation
- impl/take!
- impl/put!
- impl/close!
- Readers Queue
- Writers Queue
- Buffer ?
- Mutex
- There should not be pending readers & writers in the same time.
- There should not be any pending reads when buffer is not empty.
- There should not be any bending writes while the buffer has some room.
- Reads will consume from the buffer and remaining writes.
- Resolves pending takes with nil
- Resolves subsequent puts with nil
- No unbounded queue
- (def ^:const ^{:tag 'int} MAX-QUEUE-SIZE 1024)
- Will throw if exceeded
- Magical `go` macro.
- `go` macro works like a compiler
- `go` macro break down the clojure code into blocks
- Core.async creates a state machine from those blocks.
- The state machine can execute the blocks, pause the execution and move between them.
- Parses the Clojure code into SSA format
- SSA format represents Clojure code in blocks of instructions
- Each block has an ID and a state
- Compile SSA format back to clojure code again.
- Usually, The compiled clojure code is 50% slower.
- Executes blocks in execution threadpool by passing the necessary state to them.
- Core.asyc can start and stop blocks in any given time.
- So it can park takes and puts instead of blocking the thread.
Made with Slides.com