Skip to content

Instantly share code, notes, and snippets.

@kiennq
Last active November 30, 2019 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiennq/cfe57671bab3300d3ed849a7cbf2927c to your computer and use it in GitHub Desktop.
Save kiennq/cfe57671bab3300d3ed849a7cbf2927c to your computer and use it in GitHub Desktop.
Limit number of async processes ever created at a time
(use-package async
:ensure t
:defer 5
:init
(setq async-bytecomp-allowed-packages '(all))
:config
;; async compiling package
(async-bytecomp-package-mode t)
(dired-async-mode 1)
;; limit number of async processes
(eval-when-compile
(require 'cl-lib))
(defvar async-maximum-parallel-procs 4)
(defvar async--parallel-procs 0)
(defvar async--queue nil)
(defvar-local async--cb nil)
(advice-add #'async-start :around
(lambda (orig-func func &optional callback)
(if (>= async--parallel-procs async-maximum-parallel-procs)
(push `(,func ,callback) async--queue)
(cl-incf async--parallel-procs)
(let ((future (funcall orig-func func
(lambda (re)
(cl-decf async--parallel-procs)
(when async--cb (funcall async--cb re))
(when-let (args (pop async--queue))
(apply #'async-start args))))))
(with-current-buffer (process-buffer future)
(setq async--cb callback)))))
'((name . --queue-dispatch)))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment