Last active
November 30, 2019 04:34
-
-
Save kiennq/cfe57671bab3300d3ed849a7cbf2927c to your computer and use it in GitHub Desktop.
Limit number of async processes ever created at a time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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