How to get return value of every spawn process iteration

Q&A's, tips, howto's

How to get return value of every spawn process iteration

Postby nanxiao » Mon Jun 25, 2018 1:40 pm

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
nanxiao
 
Posts: 7
Joined: Mon Jun 25, 2018 1:28 pm

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

Postby ralph.ronnquist » Thu Jun 28, 2018 10:53 pm

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.
ralph.ronnquist
 
Posts: 178
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

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

Postby nanxiao » Fri Jun 29, 2018 9:43 am

@ralph.ronnquist:

Thanks very much for your help!
nanxiao
 
Posts: 7
Joined: Mon Jun 25, 2018 1:28 pm


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 3 guests

cron