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.1.tar.gz
(5.7 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.1.tar.gz.
File metadata
- Download URL: swiftpy_gcd-0.0.1.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05ffd7f3534d57c2b8be105fa2ce4ca920c3d215d33654bb7fd8e23f68ca4496
|
|
| MD5 |
772fc45ecffcfe61a200e2fb27edcb8e
|
|
| BLAKE2b-256 |
50e8dc10ff6d46026ea268c8eaac4cf8459dc63472f6c622eb8d0b8c9a8e29e4
|
File details
Details for the file swiftpy_gcd-0.0.1-py3-none-any.whl.
File metadata
- Download URL: swiftpy_gcd-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.6 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 |
3f4e0401a4bb41053ecbd26376874b1106d679c2f061bb5d670c88972f4a9d10
|
|
| MD5 |
bf168cb4553c2a447e432d40f38df2b5
|
|
| BLAKE2b-256 |
35693ee4ef0588c15a562a1a38b8e37023c7cf5ba78828fd020825c8ca315372
|