Skip to content

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
    • Help
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
pygolang
pygolang
  • Project
    • Project
    • Details
    • Activity
    • Releases
    • Cycle Analytics
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Charts
  • Issues 0
    • Issues 0
    • List
    • Board
    • Labels
    • Milestones
  • Merge Requests 1
    • Merge Requests 1
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Commits
  • Issue Boards
  • nexedi
  • pygolangpygolang
  • Merge Requests
  • !12

Closed
Opened Dec 10, 2020 by Kirill Smelkov@kirr
  • Report abuse
Report abuse

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

/cc @gabriel, @jerome

  • Discussion 2
  • Commits 2
  • Changes 2
Assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
0
Labels
None
Assign labels
  • View project labels
Reference: nexedi/pygolang!12
GitLab Nexedi Edition | About GitLab | About Nexedi | 沪ICP备2021021310号-2 | 沪ICP备2021021310号-7