Skip to main content

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`

Release files for pycentraldispatch 1.0.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycentraldispatch 1.0.12
File Size Uploaded
pycentraldispatch-1.0.12.tar.gz 4.7 kB Details

Release files / pycentraldispatch-1.0.12.tar.gz

Download URL pycentraldispatch-1.0.12.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1e2645bdf8e5888022ed6dafb2984fe6a41792cb16cb7ce39f6dca3a303de47a
BLAKE2b-256 checksum
How to use checksums
4ab0c2d0dc9066a89dcbf78cc4811ec924a16decf186014dcd9f294c151a94b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.7

Release history Release notifications | RSS feed

This release

1.0.12 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page