How to get return value of every spawn process iteration

Q&A's, tips, howto's
Post Reply
nanxiao
Posts: 7
Joined: Mon Jun 25, 2018 1:28 pm

How to get return value of every spawn process iteration

Post 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

ralph.ronnquist
Posts: 216
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

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

Post 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.

nanxiao
Posts: 7
Joined: Mon Jun 25, 2018 1:28 pm

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

Post by nanxiao »

@ralph.ronnquist:

Thanks very much for your help!

Post Reply