Skip to content

Commit cdd1a8c

Browse files
committed
* parallel-remote.el (parallel-remote-send): Renamed from `parallel-send'.
* parallel.el (parallel-send): New function to send data to the remote process. * test-parallel.el: Rename `parallel-send' to `parallel-remote-send'. (parallel-continue-when-executed): New test.
1 parent 6db066f commit cdd1a8c

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

parallel-remote.el

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
;;; Code:
2424

25+
(require 'cl)
26+
2527
(defvar parallel-service nil)
2628
(defvar parallel-task-id nil)
2729
(defvar parallel-client nil)
2830
(defvar parallel--executed nil)
2931
(defvar parallel-continue-when-executed nil)
3032

31-
(defun parallel-send (data)
33+
(defun parallel-remote-send (data)
3234
(process-send-string parallel-client
3335
(format "%S " (cons parallel-task-id data))))
3436

@@ -40,7 +42,7 @@
4042
:host "localhost"
4143
:family 'ipv4))
4244
(set-process-filter parallel-client #'parallel-remote--filter)
43-
(parallel-send 'code)
45+
(parallel-remote-send 'code)
4446
(when noninteractive ; Batch Mode
4547
;; The evaluation is done in the `parallel--filter' but in Batch
4648
;; Mode, Emacs doesn't wait for the input, it stops as soon as
@@ -49,17 +51,31 @@
4951
(sleep-for 10)))) ; arbitrary chosen
5052

5153
(defun parallel-remote--filter (_proc output)
52-
(parallel-send
53-
(if (or noninteractive
54-
(not debug-on-error))
55-
(condition-case err
56-
(eval (read output))
57-
(error err))
58-
(eval (read output))))
54+
(dolist (code (parallel--read-output output))
55+
(parallel-remote-send
56+
(if (or noninteractive
57+
(not debug-on-error))
58+
(condition-case err
59+
(eval code)
60+
(error err))
61+
(eval code))))
5962
(unless parallel-continue-when-executed
6063
(setq parallel--executed t)
6164
(kill-emacs)))
6265

66+
(defun parallel--read-output (output)
67+
"Read lisp forms from output and return them as a list."
68+
(loop with output = (replace-regexp-in-string
69+
"\\`[ \t\n]*" ""
70+
(replace-regexp-in-string "[ \t\n]*\\'" "" output)) ; trim string
71+
with start = 0
72+
with end = (length output)
73+
for ret = (read-from-string output start end)
74+
for data = (first ret)
75+
do (setq start (rest ret))
76+
collect data
77+
until (= start end)))
78+
6379
(provide 'parallel-remote)
6480

6581
;;; parallel-remote.el ends here

parallel.el

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
;;; Code:
2424

2525
(require 'cl)
26-
(require 'find-func)
26+
(require 'parallel-remote)
2727

2828
(defgroup parallel nil
2929
"Execute stuff in parallel"
@@ -114,7 +114,7 @@
114114
library-path (or library-path
115115
(plist-get config :library-path)
116116
(plist-get parallel-config :library-path)
117-
(find-library-name "parallel-remote")))
117+
(locate-library "parallel-remote")))
118118

119119
(let ((task (parallel--new-task))
120120
proc tunnel ssh-args)
@@ -128,6 +128,7 @@
128128
(put task 'on-event on-event))
129129
(put task 'results nil)
130130
(put task 'status 'run)
131+
(put task 'queue nil)
131132

132133
;; We need to get the tunnel if it exists so we can send the right
133134
;; `service' to the remote.
@@ -241,23 +242,19 @@ to `funcall' FUN with ENV as arguments."
241242
(defun parallel--filter (connection output)
242243
"Server filter used to retrieve the results send by the remote
243244
process and send the code to be executed by it."
244-
(loop with output = (replace-regexp-in-string
245-
"\\`[ \t\n]*" ""
246-
(replace-regexp-in-string "[ \t\n]*\\'" "" output)) ; trim string
247-
with start = 0
248-
with end = (length output)
249-
for ret = (read-from-string output start end)
250-
for data = (first ret)
251-
do (setq start (rest ret))
252-
do (parallel--process-output connection (first data) (rest data))
253-
until (= start end)))
245+
(dolist (data (parallel--read-output output))
246+
(parallel--process-output connection (first data) (rest data))))
254247

255248
(defun parallel--process-output (connection task result)
249+
(put task 'connection connection)
256250
(cond ((and (not (get task 'initialized))
257251
(eq result 'code))
258-
(process-send-string connection
259-
(parallel--call-with-env (get task 'exec-fun)
260-
(get task 'env)))
252+
(parallel-send task
253+
(get task 'exec-fun)
254+
(get task 'env))
255+
(let ((code nil))
256+
(while (setq code (pop (get task 'queue)))
257+
(parallel-send task (car code) (cdr code))))
261258
(put task 'initialized t))
262259
(t
263260
(push result (get task 'results))
@@ -298,6 +295,15 @@ result returned by exec-fun."
298295
"Stop TASK."
299296
(delete-process (get task 'proc)))
300297

298+
(defun parallel-send (task fun &optional env)
299+
"Send FUN to be evaluated by TASK in ENV."
300+
(let ((connection (get task 'connection)))
301+
(if connection
302+
(process-send-string
303+
connection
304+
(parallel--call-with-env fun env))
305+
(push (cons fun env) (get task 'queue)))))
306+
301307
(provide 'parallel)
302308

303309
;;; parallel.el ends here

test-parallel.el

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
(ert-deftest parallel-basic ()
2929
(should (= 42 (parallel-get-result (parallel-start (lambda () 42))))))
3030

31-
(ert-deftest parallel-send ()
31+
(ert-deftest parallel-remote-send ()
3232
(should (equal (parallel-get-results (parallel-start (lambda ()
33-
(parallel-send 12)
33+
(parallel-remote-send 12)
3434
42)))
3535
(list 42 12))))
3636

@@ -61,8 +61,8 @@
6161
(apply #'+
6262
(parallel-get-results
6363
(parallel-start (lambda ()
64-
(parallel-send 12)
65-
(parallel-send 42)
64+
(parallel-remote-send 12)
65+
(parallel-remote-send 42)
6666
0)
6767
:on-event
6868
(lambda (data)
@@ -84,7 +84,7 @@
8484
(ert-deftest parallel-other-library ()
8585
(let ((library (make-temp-file "parallel-")))
8686
(with-temp-file library
87-
(insert-file-contents (find-library-name "parallel-remote"))
87+
(insert-file-contents (locate-library "parallel-remote"))
8888
(insert "(defun parallel--test () 42)"))
8989
(should (equal 42
9090
(parallel-get-result
@@ -109,6 +109,12 @@
109109
;; HACKY ERT run tests in alphabetical order, hence the name.
110110
(should (eq nil parallel--tasks)))
111111

112+
(ert-deftest parallel-continue-when-executed ()
113+
(let ((task (parallel-start (lambda () 42) :continue-when-executed t :timeout 5)))
114+
(parallel-send task (lambda () (setq parallel-continue-when-executed nil)))
115+
(should (equal (list nil 42)
116+
(parallel-get-results task)))))
117+
112118
(provide 'test-parallel)
113119

114120
;;; test-parallel.el ends here

0 commit comments

Comments
 (0)