Page 1 of 1

How to get return value of every spawn process iteration

Posted: Mon Jun 25, 2018 1:40 pm
by nanxiao
Hi all,

Greeting from me! I am a newbie of newLISP, and want to implement a simple feature which launches processes according to CPU number:

Code: Select all

(import "libc.so" "sysconf")

(constant '_SC_NPROCESSORS_ONLN 503)

(define (report pid)
    (println "process: " pid " has returned"))

(define cpu-num (sysconf _SC_NPROCESSORS_ONLN))

(set 'result-array cpu-num)

(dotimes (i cpu-num)
	(spawn 'p (println i)))
	
(until (true? (sync 10 report)))

(exit)
I know following code will let p gets only one result:

Code: Select all

(dotimes (i cpu-num)
	(spawn 'p (println i)))
I want to use an array to hold every process's return value, but following code is not invalid:

Code: Select all

(set 'result-array cpu-num)

(dotimes (i cpu-num)
	(spawn (result-array i) (println i)))
So how can I get return value of every spawn process iteration in for-loop? Thanks very much in advance!

Best Regards
Nan Xiao

Re: How to get return value of every spawn process iteration

Posted: Thu Jun 28, 2018 10:53 pm
by ralph.ronnquist
You could do something like this

Code: Select all

(dotimes (i cpu-num)
   (spawn (last (push (sym (string "ret" i)) returns -1)) (println i)))
That'd make returns be a list of the return symbols, letting you do

Code: Select all

(eval (returns k))
to obtain the return value of the k:th sub process.

Or, you may want an association list, as with the following

Code: Select all

(dotimes (i cpu-num)
    (let ((p (sym (string "ret" i))))
        (push (list (spawn p (println i)) p) returns -1)))
Then you'd get the return value via

Code: Select all

(eval (lookup pid returns))
It goes without saying, that the return values won't be set until the sub processes have finished.

Re: How to get return value of every spawn process iteration

Posted: Fri Jun 29, 2018 9:43 am
by nanxiao
@ralph.ronnquist:

Thanks very much for your help!