See usage of Channel (i.e. how many items are enqueued)

I am using buffered Channels as a stream between different parts of my program, and I’d like to regularly find out how much of a buffered channel is actually used.

For example, if the channel is regularly empty, I know the producing task is a bottleneck, and if it is regularly full, I’ll know the consuming task is the bottleneck.

isready() is the closest I’ve come to getting information on this, but this can only tell me if the channel is empty. Is there a better way to do this?

And to immediately answer my own question:

To get a fraction of the channel used, try:

length(ch.data) / ch.sz_max

There’s probably issues with threadsafety when accessing the length(ch.data), but it’s good enough for my use case.

There is a non-exported function Base.n_avail for querying the length of current data. It works for both buffered and unbuffered channels.

1 Like