Go Concurrency and Patterns – Part 2

I dove into the kinds of concurrency use cases that I need (or think I need) and those that Go provides.

Generally I think I need two types:

1. Doing a system call (ie to a python script) that might timeout on a device (like an I2C bus) that by it’s nature is synchronous. So while the underlying operations on the bus are still sequential, I’d like to catch any hung I2C operations and not hang the whole system.

2. Since the Beaglebone has a Whole Bunch of Busses, I’d like to operate on them concurrently.

Go does this, with Goroutines and channels.

The first type is simply a call to a Goroutine with a timeout. Without a timeout the Goroutine might actually hang and we’d never know. The basic structure is formed in a select-case statement, and there isĀ an example here.

The select waits (blocks, actually) until the timeout value is reached, or the response is received on the channel. This is ok per-bus, since we can’t really muck around with something like I2C or SPI while the bus is busy.

The second type is simply calling these bus controller Goroutines by placing commands into a command channel and getting status info back on a status channel. These would be created at startup, and persist until shutdown.

Think of a bus controller Goroutine as having two kinds of interfaces, one for synchronous bus operations, and one for command/status handling that is asyncronous from the bus that’s being controlled.

In this way the bus controller can be polled or commanded at any update frequency, and the logic internal to the routine buffers (or more likely overwrites) the next set of commands to be issued on the physical bus, or get status at any time from any module, from the internal state copy of the modules under it’s control. If you get the whole idea of I2C registers, it helps.

From the point of view of the loop that’s calling all of these bus controllers, it’s still synchronous, since it just plods through it’s instructions to get input, do some logic, and set some outputs.

[edit: this was never posted on the old site, I found it sitting as an unfinished draft]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.