A simple Grand Central Dispatch (GCD) inspired API for python.
Project description
PyCentralDispatch
A Grand Central Dispatch (GCD) inspired API for python. Not optimal, mostly just for convenience.
Inspired by Mike Ash's article.
Install
Install the package using pip.
python3 -m pip install pycentraldispatch
API
The general behavior of this API is inspired by Apple's Grand Central Dispatch (GCD). The API allows for dispatching
tasks (functions) synchronously or asynchronously. There are two flavors of queues: serial and concurrent.
serial - Tasks will be executed one-at-a-time in the order they are queued.
concurrent - Tasks may be executed in parallel, but they will be started in the order they are queued.
Additionally, the API provides access to a global queue, which is a default singleton concurrent queue that
can be shared across the application.
The primary APIs are dispatch_sync and dispatch_async, which dispatch the task onto a queue, either
synchronously or asynchronously. The primary difference is that synchronous tasks do not return until the
task is completed. Asynchronous tasks will be run in the background and return from the dispatch call
immediately.
Global queue
A shared concurrent queue singleton for access across the application.
from pycentraldispatch import PyCentralDispatch
# This same singleton queue can be used anywhere in the application.
global_queue = PyCentralDispatch.global_queue()
def some_function():
print 'hello'
global_queue.dispatch_sync(some_function) # Prints "hello"
Serial queues
Serial queues do not provide any parallelization. They execute tasks one-at-a-time in the order they were received.
from pycentraldispatch import PyCentralDispatch
local_serial_queue = PyCentralDispatch.create_queue(is_serial_queue=True)
def some_function_with_parameters(a, b):
print a + b
local_serial_queue.dispatch_async(some_function_with_parameters, args=(3, 5)) # Prints `8`
Concurrent queues
Concurrent queues allow for tasks to execute in parallel. However the tasks are started in the order they were queued.
from pycentraldispatch import PyCentralDispatch
local_concurrent_queue = PyCentralDispatch.create_queue() # Defaults to concurrent queue.
def some_function_with_parameters(a, b=5):
print a + b
local_concurrent_queue.dispatch_async(some_function_with_parameters, args=(3,), kwargs={'b' : 3}) # Prints `6`
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
File details
Details for the file pycentraldispatch-1.0.12.tar.gz.
File metadata
- Download URL: pycentraldispatch-1.0.12.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e2645bdf8e5888022ed6dafb2984fe6a41792cb16cb7ce39f6dca3a303de47a
|
|
| MD5 |
ee1e8340693805e37824290d369dd9f4
|
|
| BLAKE2b-256 |
4ab0c2d0dc9066a89dcbf78cc4811ec924a16decf186014dcd9f294c151a94b3
|