https://github.com/ptsochantaris/semalot
An elementary counting semaphore for async tasks in Swift
https://github.com/ptsochantaris/semalot
concurrency ios macos swift threading
Last synced: 4 months ago
JSON representation
An elementary counting semaphore for async tasks in Swift
- Host: GitHub
- URL: https://github.com/ptsochantaris/semalot
- Owner: ptsochantaris
- License: mit
- Created: 2023-08-08T22:43:08.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-11T23:06:52.000Z (8 months ago)
- Last Synced: 2025-01-30T22:41:35.730Z (5 months ago)
- Topics: concurrency, ios, macos, swift, threading
- Language: Swift
- Homepage:
- Size: 26.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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!
[](https://swiftpackageindex.com/ptsochantaris/key-vine) [](https://swiftpackageindex.com/ptsochantaris/key-vine)
Currently used in
- [Trailer](https://github.com/ptsochantaris/trailer)
- [Trailer-CLI](https://github.com/ptsochantaris/trailer-cli)
- [Gladys](https://github.com/ptsochantaris/gladys)Detailed docs [can be found here](https://swiftpackageindex.com/ptsochantaris/semalot/documentation)
## 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.