Skip to content

Instantly share code, notes, and snippets.

@kiennq
Last active June 23, 2025 12:56
Show Gist options
  • Select an option

  • Save kiennq/cfe57671bab3300d3ed849a7cbf2927c to your computer and use it in GitHub Desktop.

Select an option

Save kiennq/cfe57671bab3300d3ed849a7cbf2927c to your computer and use it in GitHub Desktop.

Revisions

  1. kiennq revised this gist Nov 30, 2019. 1 changed file with 9 additions and 10 deletions.
    19 changes: 9 additions & 10 deletions emacs-async-config.el
    Original file line number Diff line number Diff line change
    @@ -12,21 +12,20 @@
    (require 'cl-lib))
    (defvar async-maximum-parallel-procs 4)
    (defvar async--parallel-procs 0)
    (defvar async--queue nil
    "Queue of (func callback) that is waited for being dispatched async.")
    (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)
    (setq async--cb callback)
    (funcall orig-func func
    (lambda (re)
    (message "done: %s" re)
    (cl-decf async--parallel-procs)
    (when async--cb (funcall async--cb re))
    (when-let ((args (pop async--queue)))
    (apply #'async-start args))))))
    (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)))
    )
  2. kiennq created this gist Nov 22, 2019.
    32 changes: 32 additions & 0 deletions emacs-async-config.el
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    (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
    "Queue of (func callback) that is waited for being dispatched async.")
    (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)
    (setq async--cb callback)
    (funcall orig-func func
    (lambda (re)
    (message "done: %s" re)
    (cl-decf async--parallel-procs)
    (when async--cb (funcall async--cb re))
    (when-let ((args (pop async--queue)))
    (apply #'async-start args))))))
    '((name . --queue-dispatch)))
    )