Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycentraldispatch-1.0.11.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycentraldispatch-1.0.11-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file pycentraldispatch-1.0.11.tar.gz.

File metadata

  • Download URL: pycentraldispatch-1.0.11.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1

File hashes

Hashes for pycentraldispatch-1.0.11.tar.gz
Algorithm Hash digest
SHA256 53e464eaeedb37462d7867d48b09ef3af93a78cb10e1a2f123582f36aa43911a
MD5 ebbd574fa57cf89d402e879fb2b70fde
BLAKE2b-256 fd16849c89f590803ba3641aecdc28e170f2f1e5c12f6b430df6dec600ba4a5f

See more details on using hashes here.

File details

Details for the file pycentraldispatch-1.0.11-py3-none-any.whl.

File metadata

  • Download URL: pycentraldispatch-1.0.11-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1

File hashes

Hashes for pycentraldispatch-1.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 9b20c0cc6d149bdeeb787e8e2868e11654e117a56d37f110bf166237a1c5d30a
MD5 e6adc02860e1f4eaa2c4cc6ea00f8192
BLAKE2b-256 34ef44cacd204908ec09e9767bcdc17a004dd4e735faf5bd594936c8212d2404

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page