Semalot

1.0.4

An elementary counting semaphore for async tasks in Swift
ptsochantaris/semalot

What's New

Version 1.0.4

2023-10-02T10:18:56Z

Added waitForAllTickets convenience method

Logo

Semalot

πŸ‘¨ "Semalot!" πŸ‘¨β€πŸ¦± "Semalot!!" πŸ‘΄ "Semalot!!!" πŸ€¦β€β™€οΈ "It's only a counter…" πŸ‘¨πŸ‘¨β€πŸ¦±πŸ‘΄ "Shh!!!!"

An elementary counting semaphore for async tasks in Swift, which I use a lot in my code so I thought I should turn it into a package!

Currently used in

Detailed docs can be found here

Overview

Does what it says on the tin. It's simple and efficient, does not use any dispatch locks, and does not cause any Task queue congestion.

let maxConcurrentOperations = Semalot(tickets: 3)

try await withThrowingTaskGroup { group in
    for request in lotsOfRequests {
        await maxConcurrentOperations.takeTicket()
        group.addTask {
            let data = try await urlSession.data(for: request).0
            await doThings(with: data)
            maxConcurrentOperations.returnTicket()
        }
    }
}

License

Copyright (c) 2023 Paul Tsochantaris. Licensed under the MIT License, see LICENSE for details.

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

Last updated: Sat Mar 16 2024 02:49:55 GMT-0900 (Hawaii-Aleutian Daylight Time)