Is it possible to define a workflow with 2 activities that are bound to 2 workers respectively?

I’ve confirmed that a workflow can specify a task queue by using ActivityOptions and creating a new context for each activity invocation.

As per the title, I’m thinking of whether I can define a workflow with 2 activities that are bound to 2 workers respectively.

When running the worker/main.go like the following, it initiates only w1. Apparently, it’s due to worker.InterruptCh() as the argument of the Run function.

func main() {
	c, err := client.Dial(client.Options{})
	if err != nil {
		log.Fatalln("Unable to create client", err)
	}
	defer c.Close()

	w1 := worker.New(c, MyTaskQueue1, worker.Options{})
	w2 := worker.New(c, MyTaskQueue2, worker.Options{})

	w1.RegisterWorkflow(MyWorkflow)
	w1.RegisterActivity(MyActivity1)
	w2.RegisterActivity(MyActivity2)

	err = w1.Run(worker.InterruptCh())
	if err != nil {
		log.Fatal("Unable to start worker", err)
	}

	err = w2.Run(worker.InterruptCh())
	if err != nil {
		log.Fatal("Unable to start worker", err)
	}

Apparently, a worker executes tasks so having multiple workers help execute tasks faster if tasks are enormous. For example, a workflow that spawns child workflows with many activities. Going back to my question, Is it possible to define a workflow with 2 activities that are bound to 2 workers respectively?

You can either use Worker.Start instead of Worker.Run or call Worker.Run for each worker in its own goroutine.

Apparently, a worker executes tasks so having multiple workers help execute tasks faster if tasks are enormous.

I don’t think this is necessarily true. Multiple workers and queues allow specifying different parallelism and rate limits per queue. But I would recommend starting from a single worker and only going to multiple if you start running into performance issues.

1 Like