branch: externals/el-job
commit 24016a05bd5bf61a20a88ea79f63b0fb1881b55b
Merge: 850391b0e1 d79a70e21a
Author: Martin Edström <meedst...@runbox.eu>
Commit: GitHub <nore...@github.com>

    Merge pull request #5 from zawatton21/fix/windows-core-detection
    
    Fix child process error on Windows by improving core count detection
---
 el-job.el | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/el-job.el b/el-job.el
index b0b359d6f4..e2512a6fbc 100644
--- a/el-job.el
+++ b/el-job.el
@@ -293,9 +293,25 @@ See subroutine `el-job-child--zip' for details."
       (setq merged (el-job-child--zip (pop lists) merged)))
     merged))
 
+(defun el-job--windows-cores ()
+  "Try to get number of physical cores on Windows system."
+  (with-temp-buffer
+    (call-process "cmd.exe" nil t nil "/C" "wmic CPU get NumberOfCores /value")
+    (goto-char 1)
+    (or (and (re-search-forward "NumberOfCores=\\([0-9]+\\)" nil t)
+             (number-to-string (match-string 1)))
+        1)))
+
 
 ;;; Main logic
 
+(defcustom el-job-max-cores nil
+  "A limit on the number of subprocesses for one job.
+Windows can get \"error: Could not create child process\" if making too
+many processes, so capping it can help."
+  :type '(choice integer (const :tag "Auto" nil))
+  :group 'processes)
+
 (defvar el-job--all-jobs (make-hash-table :test #'eq)
   "Table of all el-job objects.")
 
@@ -468,10 +484,14 @@ For debugging, see these commands:
           (when do-exec
             (setf .callback callback)
             ;; Prevent spawning a dozen processes when we need only one or two
-            (let ((machine-cores (max 1 (1- (num-processors)))))
-              (setf .n-cores-to-use (if (length< .queued-inputs machine-cores)
-                                        (length .queued-inputs)
-                                      machine-cores))
+            (let ((machine-cores (max 1 (or el-job-max-cores
+                                            (and (eq system-type 'windows-nt)
+                                                 (- (el-job--windows-cores) 1))
+                                            (- (num-processors) 1)))))
+              (setf .n-cores-to-use
+                    (if (length< .queued-inputs machine-cores)
+                        (length .queued-inputs)
+                      machine-cores))
               (when (or (length< .ready .n-cores-to-use)
                         (not (cl-every #'process-live-p .ready)))
                 (setq do-respawn t)))

Reply via email to