All Projects → divs1210 → functional-core-async

divs1210 / functional-core-async

Licence: EPL-1.0 License
almost, but not quite, entirely unlike core.async

Programming Languages

clojure
4091 projects

Labels

Projects that are alternatives of or similar to functional-core-async

csp
Because Security Matters, and Web libraries, tools, and projects, should be more informative about their state.
Stars: ✭ 15 (-11.76%)
Mutual labels:  csp
pyGRETA
python Generator of REnewable Time series and mAps
Stars: ✭ 27 (+58.82%)
Mutual labels:  csp
dhroraryus
Dhroraryus generates schedules intelligently according to one's constraints and preferences
Stars: ✭ 16 (-5.88%)
Mutual labels:  csp
SOMns
SOMns: A Newspeak for Concurrency Research
Stars: ✭ 62 (+264.71%)
Mutual labels:  csp
plg system httpheader
This is a Joomla Plugin that provides setting of HTTP Headers
Stars: ✭ 19 (+11.76%)
Mutual labels:  csp
CrySPY
CrySPY is a crystal structure prediction tool written in Python.
Stars: ✭ 58 (+241.18%)
Mutual labels:  csp
cb-tumblebug
Cloud-Barista Multi-Cloud Infra Management Framework
Stars: ✭ 33 (+94.12%)
Mutual labels:  csp
csp.js
📺 CSP for vanilla JavaScript
Stars: ✭ 45 (+164.71%)
Mutual labels:  csp
nuxt-security
Module for Nuxt.js to configure security headers and more
Stars: ✭ 46 (+170.59%)
Mutual labels:  csp
bookmarklets-context-menu
WebExtension allow to execute bookmarklets as privileged scripts
Stars: ✭ 67 (+294.12%)
Mutual labels:  csp
django-http2-middleware
⚡️Django middleware to automatically send preload headers before views runs, enabling faster HTTP2 server-push (with CSP support).
Stars: ✭ 65 (+282.35%)
Mutual labels:  csp
AspNetCoreMvcAngular
ASP.NET Core MVC with angular in MVC View OpenID Connect Hybrid Flow
Stars: ✭ 54 (+217.65%)
Mutual labels:  csp
csp
A library for Communicating Sequential Processes in Node.js, built on top of async/await
Stars: ✭ 59 (+247.06%)
Mutual labels:  csp
go-csp-collector
A CSP collector written in Golang
Stars: ✭ 74 (+335.29%)
Mutual labels:  csp
gatsby-plugin-csp
A Gatsby plugin which adds strict Content Security Policy to your project.
Stars: ✭ 40 (+135.29%)
Mutual labels:  csp
AspNetCore6Experiments
ASP.NET Core Blazor BFF with Azure AD and Razor page
Stars: ✭ 43 (+152.94%)
Mutual labels:  csp
frontreport
Simple frontend logging collector written in Go
Stars: ✭ 23 (+35.29%)
Mutual labels:  csp
trusted-crypto
A native library implementing of cryptography, ciphers, PKI and the formats that are used in PKI applications.
Stars: ✭ 19 (+11.76%)
Mutual labels:  csp
KO--CSP
更新pat、csp以及研究生上机考试的刷题笔记
Stars: ✭ 23 (+35.29%)
Mutual labels:  csp
pool
A highly flexible process pooling library for Node.js
Stars: ✭ 18 (+5.88%)
Mutual labels:  csp
He had found a Nutri-Matic machine which had provided him with
a plastic cup filled with a liquid that was almost, but not quite,
entirely unlike tea.

The way it functioned was very interesting. When the Drink button
was pressed it made an instant but highly detailed examination of
the subject's taste buds, a spectroscopic analysis of the subject's
metabolism and then sent tiny experimental signals down the neural
pathways to the taste centers of the subject's brain to see what
was likely to go down well. However, no one knew quite why it did
this because it invariably delivered a cupful of liquid that was
almost, but not quite, entirely unlike tea.

from The Hitchhiker's Guide to the Galaxy

functional-core-async

CSP - S = Communicating Processes = Green Threads!

Why

  • It makes writing concurrent software much simpler by getting data out of callbacks through the use of magic portals called channels.
  • It provides green threads via go blocks that can park and be multiplexed over JVM threads, and communicate over channels.
  • It can be ported to other systems and languages in a rather straightforward manner. For example, here is a javascript port.

Differences from core.async

  • CSP - S: making callbacky code look sequential requires access to compiler. This is avoided to aid portability.
  • >! and <! are implemented as functions and take callbacks. These should be top-level in their go blocks.
  • go blocks (green 'threads') are multiplexed over n Clojure futures, where n = number of cores.
  • the go macro can only park a single >! or <! that is returned from its body.

Intro Video

Currently the JS implementation is far ahead of this implementation. You can see a demo of it here.

Usage

Let's look at an everyday async call to the database to fetch a string corresponding to the given id -

Simple Callback

;; from examples.clj
(defn async-callback []
  (get-user-from-db :user0
                    #(let [resp %
                           massaged-resp (seq resp)]
                       (println "via cb:" massaged-resp)
                       (println "but can't access outside callback :("))))

The function fires a query to the db and immediately returns nil.

In this implementation, the response is locked inside the callback and whatever code needs access to it should be put inside that callback.

This leads to what is called callback-hell, which can be escaped with the help of those handy magic portals we talked about.

Channels to The Rescue

;; from examples.clj
(defn async-ch []
  (let [ch (chan)]
    (get-user-from-db :user1
                      #(>!! ch %))
    (println "but blocks on accessing response :(")
    (let [resp (<!! ch)
          massaged-resp (seq resp)]
      (println "via ch:" massaged-resp)
      massaged-resp)))

In this version, we have modified the callback to just put the response onto the channel ch. The db call is made asynchronously and the call to print is executed immediately afterwards.

When we get our response from the channel, however, the thread blocks, waiting for the callback to complete and ch to receive a value.

We then take the return value from ch and voila! We have the response out of the callback! It's unfortunate that our function has now become blocking, though.

Fully Async

;; from examples.clj
(defn async-ch-go []
  (let [ch (chan)]
    (get-user-from-db :user1
                      #(>!! ch %))
    (go
      (<! ch
          #(let [resp %
                 massaged-resp (seq resp)]
             (println "via ch/go:" massaged-resp)
             (println "and didn't block!")
             massaged-resp)))))

which can also be written as

(defn async-ch-go []
  (let [ch (chan)]
    (get-user-from-db :user1
                      #(>!! ch %))
    (go<! [resp ch]
      (let [massaged-resp (seq resp)]
        (println "via ch/go:" massaged-resp)
        (println "and didn't block!")
        massaged-resp))))

This version is only slightly different to the previous one. We put the fn body after the async call to the database inside a go block, which is executed on the async-executor thread, immediately returning a channel.

We can then call (<!! c) on that channel to get massaged-resp. So now we have sequential code instead of nested hell while being fully async!

The Hot Dog Machine Process You’ve Been Longing For

Here's a port of the Hot Dog Machine

(defn hot-dog-machine
  [in out hot-dogs-left]
  (when (> hot-dogs-left 0)
    (go<! [input in]
      (if (= 3 input)
        (go>! [out "hot dog"]
          (hot-dog-machine in out (dec hot-dogs-left)))
        (go>! [out "wilted lettuce"]
          (hot-dog-machine in out hot-dogs-left))))))

Let's give it a try:

(let [in (chan)
      out (chan)
      _ (hot-dog-machine in out 2)]
  (>!! in "pocket lint")
  (println (<!! out))

  (>!! in 3)
  (println (<!! out))

  (>!! in 3)
  (println (<!! out)))
; => wilted lettuce
; => hotdog
; => hotdog

There's also goloop and goconsume. Read the source to know more!

TODO

  • preserve thread-local bindings in go blocks
  • optimize scheduler: replace round-robin scheduling with per-channel queues and listeners
  • close!
  • alts!

License

Copyright © 2017 Divyansh Prakash

Distributed under the Eclipse Public License either version 1.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].