emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Threading IO-bound functions


From: Elias Mårtenson
Subject: Threading IO-bound functions
Date: Thu, 15 Dec 2016 12:01:51 +0800

The number one function that I call that sometimes hang for a significant amount of time is ‘gnus’. I decided to try running it in a thread, and it worked surprisingly well. Initial loading of the messages can now be done in the background.

To prevent myself from running this function more than once at the same time, I created a wrapper function for this, and I have extracted it into a macro.

I'd like to have people's opinions on this strategy, and if it might be reasonable to default ‘gnus’ to do this when run on Emacs versions with concurrency support.

(defmacro define-background-function-wrapper (bg-function fn)
  (let ((is-loading-sym (intern (concat "*" (symbol-name bg-function) "-is-loading*"))))
    `(progn
       (defvar ,is-loading-sym nil)
       (defun ,bg-function ()
         (interactive)
         (when ,is-loading-sym
           (message ,(concat (symbol-name fn) " is already loading")))
         (setq ,is-loading-sym t)
         (make-thread (lambda ()
                        (unwind-protect
                            (,fn)
                          (setq ,is-loading-sym nil))))))))

It's invoked like ‘(define-background-function-wrapper bg-gnus gnus)’. This will then define the function ‘bg-gnus’ that runs ‘gnus‘ in a thread.

Regards,
Elias

reply via email to

[Prev in Thread] Current Thread [Next in Thread]