Swift's Grand Central Dispatch (GCD) implemented in Python
Project description
swift.py
A Python implementation of Apple's Grand Central Dispatch (GCD) pattern, bringing efficient concurrent and parallel execution to Python applications (for no good reason).
Installation
# Using uv (recommended - faster)
uv pip install swiftpy-gcd
# Using pip
pip install swiftpy-gcd
Usage
from swift import (
DispatchQueue,
DispatchQueueAttributes,
DispatchWorkItem
)
# Create a concurrent queue for I/O operations
io_queue = DispatchQueue(
label="com.example.io",
attributes=DispatchQueueAttributes.CONCURRENT
)
# Async execution
io_queue.async_(lambda: print("Async work"))
# With a work item
work = DispatchWorkItem(block=lambda: print("Work item"))
io_queue.async_(work)
# Delayed execution
io_queue.async_after(5.0, lambda: print("Delayed work"))
# CPU-bound parallel processing
cpu_queue = DispatchQueue(
label="com.example.cpu",
attributes=DispatchQueueAttributes.CONCURRENT_WITH_MULTIPROCESSING
)
# This will run in true parallel on multiple cores
cpu_queue.async_(lambda: process_large_dataset())
# Synchronous execution
result = cpu_queue.sync(lambda: compute_something())
# Serial queue for ordered operations
serial_queue = DispatchQueue(label="com.example.serial")
serial_queue.async_(lambda: print("First"))
serial_queue.async_(lambda: print("Second")) # Guaranteed to run after First
Features
- Dispatch Queues: Both serial and concurrent execution
- Multiprocessing Support: True parallel execution for CPU-bound tasks
- Work Items: Cancellable tasks with completion handlers
- Delayed Execution: Schedule work to run after a delay
- Thread Safety: Built-in synchronization mechanisms
- Global Queues: Pre-configured queues for common scenarios
- Main queue for UI operations
- Global concurrent queue for I/O
- Global CPU queue for parallel processing
Requirements
- Python 3.8+
- No external dependencies
Performance
- Efficient thread/process pool management
- Smart handling of CPU vs I/O bound tasks
- Low overhead task scheduling
- Automatic worker scaling based on system capabilities
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
swiftpy_gcd-0.0.2.tar.gz
(5.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swiftpy_gcd-0.0.2.tar.gz.
File metadata
- Download URL: swiftpy_gcd-0.0.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
133c884d45565b39e78fc1e263d5ff69d4c9403766e9f8a17ce5a4b685f63c4f
|
|
| MD5 |
c1ff7a9bde7b63dd6ef01e053516d156
|
|
| BLAKE2b-256 |
cf7c84739e8d3451cc10d0264b553641de3fdc73f27571aae80bba731870f575
|
File details
Details for the file swiftpy_gcd-0.0.2-py3-none-any.whl.
File metadata
- Download URL: swiftpy_gcd-0.0.2-py3-none-any.whl
- Upload date:
- Size: 12.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d0a5b6cfd1629c29161ab3003f97f10959fd91eaff7cc0711cd199ce1319a5a
|
|
| MD5 |
aafca197d43b144298e8be8652b0ff70
|
|
| BLAKE2b-256 |
300e4a6155acb4a3f0b5d80b16779afdac3f883915e39c133a44a24f2389e475
|