Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Register
  • Sign in
  • pygolang pygolang
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 1
    • Merge requests 1
  • Deployments
    • Deployments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • Repository
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar
  • nexedinexedi
  • pygolangpygolang
  • Merge requests
  • !12

sync.WorkGroup: Provide "with" support

  • Review changes

  • Download
  • Patches
  • Plain diff
Closed Kirill Smelkov requested to merge kirr/pygolang:y/workgroup-with into master Dec 10, 2020
  • Overview 2
  • Commits 2
  • Changes 2

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".

/cc @gabriel, @jerome

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: y/workgroup-with
GitLab Nexedi Edition | About GitLab | About Nexedi | 沪ICP备2021021310号-2 | 沪ICP备2021021310号-7