vladopajic / go-actor-examples Goto Github PK
View Code? Open in Web Editor NEWfrom zero to go-actor master
Home Page: https://github.com/vladopajic/go-actor
License: GNU General Public License v3.0
from zero to go-actor master
Home Page: https://github.com/vladopajic/go-actor
License: GNU General Public License v3.0
Hello, I've been looking at the library and I have a query. Consider the following example
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/vladopajic/go-actor/actor"
)
type produceWorker struct {
mailbox actor.Mailbox[string]
num int
l []string
}
type consumerWorker struct {
inMbx actor.Mailbox[string]
id int
}
func (w *produceWorker) DoWork(c actor.Context) actor.WorkerStatus {
if w.num < len(w.l) {
w.mailbox.SendC() <- w.l[w.num]
w.num++
return actor.WorkerContinue
}
return actor.WorkerEnd
}
func (w *consumerWorker) DoWork(c actor.Context) actor.WorkerStatus {
select {
case s := <-w.inMbx.ReceiveC():
fmt.Printf("consumed %s \t(worker %d)\n", s, w.id)
return actor.WorkerContinue
case <-c.Done():
return actor.WorkerEnd
}
}
func main() {
mailbox := actor.NewMailbox[string]()
pw := &produceWorker{mailbox: mailbox, l: []string{"A", "B", "C"}}
cw := &consumerWorker{inMbx: mailbox, id: 1}
a := actor.Combine(
mailbox,
actor.New(pw),
actor.New(cw),
)
a.Start()
defer a.Stop()
<-WaitForTermination()
}
func WaitForTermination() <-chan struct{} {
sig := make(chan os.Signal, 1)
done := make(chan struct{})
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sig
close(done)
}()
return done
}
I am trying to replicate the examples in the example folder (btw it looks as if the examples are outdated as they use mailbox.Send/SendReceiver but these methods don't exist?), but what i would like to achieve is for the producer to sent strings into the mailbox (A, B, C above) and then terminate, and for the consumer to grab these strings, print them, and then terminate. The example above works in that the strings are generated and printed, but afterwards the program doesn't terminate. Could you point me into the direction of what I'm missing here? Thanks :)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.