sync.WorkGroup: Provide "with" support
So that it becomes possible to write
with WorkGroup(ctx) as wg:
wg.go(f1)
wg.go(f2)
instead of
wg = WorkGroup(ctx)
defer(wg.wait)
wg.go(f1)
wg.go(f2)
or
wg = WorkGroup(ctx)
wg.go(f1)
wg.go(f2)
wg.wait()
This is sometimes handy and is referred to as "structured concurrency" in Python world.
sync.Sema, sync.Mutex, sync.RWMutex already support "with". sync.WaitGroup is imho too low-level, but we might consider adding "with" support for it in the future as well.
In general pygolang way is to used defer instead of plugging all classes
with __enter__
/__exit__
"with" support, but for small well-known class of
concurrency-related things its seems "with" support is worth it:
- having "with" for sync.Mutex+co allows it to be used as a drop-in replacement instead of threading.Lock, and
- having "with" for sync.WorkGroup - the most commonly-used tool to spawn jobs and wait for their completion - makes it on-par with "structured concurrency".