Debian Codegraph @

demo/taskvent.r

raw symbols SCIP
1 ### Task ventilator send to two 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 sender <- zmq.socket(context, ZMQ.ST()$PUSH)
12 zmq.bind(sender, "tcp://*:5557")
13 sink <- zmq.socket(context, ZMQ.ST()$PUSH)
14 zmq.connect(sink, "tcp://localhost:5558")
15
16 ### Send sink.
17 readline("Press Enter when workers are ready:")
18 cat("Sending tasks to workers ...\n")
19 zmq.send(sink, "0")
20
21 ### Send 100 tasks.
22 set.seed(1234)
23 total.msec <- 0
24 for(i in 1:100){
25 workload <- as.integer(runif(1, 1, 30))
26 total.msec <- total.msec + workload
27 string <- sprintf("%d", workload)
28 zmq.send(sender, string)
29 Sys.sleep(runif(1, 3, 5))
30 }
31 cat("Total expected cost: ", total.msec, " msec\n", sep = "")
32
33 ### Finish.
34 zmq.close(sink)
35 zmq.close(sender)
36 zmq.ctx.destroy(context)