Debian Codegraph @

demo/taskwork.r

raw symbols SCIP
1 ### Task workers as in the ZeroMQ guide.
2 # SHELL> Rscript taskwork.r &
3 # SHELL> Rscript taskvent.r; Rscript tasksink.r
4 ### Remember to kill two worker processors at the end, such as
5 # SHELL> ps -x|grep "file=task.*\.r"|sed "s/\(.*\) pts.*/\1/"|xargs kill -9
6
7 suppressMessages(library(pbdZMQ, quietly = TRUE))
8
9 ### Initial.
10 context <- zmq.ctx.new()
11 receiver <- zmq.socket(context, ZMQ.ST()$PULL)
12 zmq.connect(receiver, "tcp://localhost:5557")
13 sender <- zmq.socket(context, ZMQ.ST()$PUSH)
14 zmq.connect(sender, "tcp://localhost:5558")
15
16 ### Process tasks forever.
17 while(TRUE){
18 string <- zmq.recv(receiver)
19 # cat(string$buf, "\n")
20 Sys.sleep(as.integer(string$buf) / 1000)
21 zmq.send(sender, paste("sleep", string$buf, "sec at", Sys.getpid()))
22 }
23
24 ### Finish.
25 zmq.close(receiver)
26 zmq.close(sender)
27 zmq.ctx.destroy(context)