Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
pygolang pygolang
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • Operations
    • Operations
    • Incidents
  • Analytics
    • Analytics
    • Repository
    • Value Stream
  • Snippets
    • Snippets
  • Members
    • Members
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar
  • nexedi
  • pygolangpygolang
  • Merge requests
  • !12

Closed
Created Dec 10, 2020 by Kirill Smelkov@kirrOwner
  • Report abuse
Report abuse

sync.WorkGroup: Provide "with" support

  • 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
Reviewer
Request review from
None
Milestone
None
Assign milestone
Time tracking
Source branch: y/workgroup-with
GitLab Nexedi Edition | About GitLab | About Nexedi | 沪ICP备2021021310号-2 | 沪ICP备2021021310号-7