Skip to main content

What is semisync.py?

Some problems are best solved synchronously, while others are a better fit for the asynchronous paradigm. Most problems fall somewhere in between – they could benefit from asynchronous execution, but require some events to happen in a certain order. This module seeks to make blending the two paradigms a bit easier by introducing a concept of dependencies. If one process must not run until another process has completed, that process is said to be “dependent” on the second process. Semisync.py was built using python’s multiprocessing library and a liberal dose of decorator syntax.

Installation

Install via pip

sudo pip install semisync

or via setup.py

sudo python setup.py install

Let’s See Some Code

from semisync import semisync
from multiprocessing import Manager
from random import random, randint
from time import sleep

# shared data between processes
shared = Manager().Namespace()

# a demo callback function
def output(field, value):
  print field + ": $" + str(value)

# simple callback syntax
@semisync(callback=output)
def revenue():
  # simulated api call
  sleep(random())
  shared.revenue = randint(1, 1000)
  return "Revenue", shared.revenue

@semisync(callback=output)
def expenses():
  # simulated api call
  sleep(random())
  shared.expenses = randint(1, 500)
  return "Expenses", shared.expenses

# will run only when revenue() and expenses() have completed
@semisync(callback=output, dependencies=[revenue, expenses])
def profit():
  shared.profit = shared.revenue - shared.expenses
  return "Profit", shared.profit

# queue function calls
revenue()
expenses()
profit()

# executes queued calls semi-synchronously
semisync.begin()

To repeat the process, simply clear the cache of function calls by using semisync.clear() after each iteration

for i in range(10):
  revenue()
  expenses()
  profit()
  semisync.begin()
  semisync.clear()

In this simple example, moving from synchronous to semi-synchronous execution cuts the average execution time from 1.00 seconds to .700 seconds. And although the example used is trivial, dependency trees can be arbitrarily complex.

Additional Notes

In order to make the module more flexible, few assumptions are made about how you choose to deal with shared data. Although Manager() from the multiprocessing library is used in the example, you’re free to use whatever format you desire. You’re also in charge of locking shared data if multiple processes access the same variable. With great flexibility comes great responsibility.

Metadata

Release files for semisync 0.1

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

Source distribution (sdist)

Source distribution for semisync 0.1
File Size Uploaded
semisync-0.1.tar.gz 1.8 kB Details

Release files / semisync-0.1.tar.gz

Download URL semisync-0.1.tar.gz
Size 1.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fd0f726790637eb56fd7f2513202d41ac3e362887aecec577310f82c1d6918fa
BLAKE2b-256 checksum
How to use checksums
9909f0237ff501754852f8188359580be4527fdeaf0cdc55e3ed1a6e2623a364
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1 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