Skip to main content

Parallel programming for software engineers

Project description

asyncframes is a coroutine library for Python and a reference implementation of the Frame Hierarchy Programming Model (FHM). The goal of FHM is to help programmers design clean and scalable parallel programs. The main features of asyncframes are:

  • Hierarchical code design

  • Inherent and scalable parallelism

  • Architecture independence

  • Extensibility through frame classes (a class whose lifetime is bound to the execution of a frame)

Introduction

In the Frame Hierarchy Programming Model (FHM) a program is represented as a dynamic tree of frames. A frame is a suspendable function (a coroutine) with an object oriented context (the frame class) that only exists until the function returns. Frames can be used to represent both temporal processes (using the coroutine) and physical or conceptual objects (using the frame class).

Each FHM program has exactly one root frame. The root frame can recursively spawn child frames. Each child frame runs in parallel unless it’s awaiting another frame or an awaitable event. Frames of type Frame run on a single thread. They use cooperative multitasking to simulate parallelism. Frames of type PFrame run on any of the threads available in the event loop’s thread pool. Frame and PFrame are frame classes. They can be sub-classed to create specialized frame classes with encapsulated data.

Installation

asyncframes can be installed via pip:

pip install asyncframes

asyncframes requires an event loop to suspend execution without blocking the operating system. The default event loop is asyncframes.asyncio_eventloop.EventLoop. It doesn’t depend on any Python packages besides the builtin asyncio package. Some frameworks, like Qt, use their own event loops. When using such frameworks, the framework’s event loop should be reused for asyncframes by implementing the asyncframes.AbstractEventLoop interface.

Examples

Here is a minimal example of using asyncframes:

from asyncframes import Frame
from asyncframes.asyncio_eventloop import EventLoop

@Frame
async def main_frame():
    print("Hello World!")

loop = EventLoop()
loop.run(main_frame)

Here is an example of suspending a frame:

from asyncframes import Frame, sleep
from asyncframes.asyncio_eventloop import EventLoop

@Frame
async def main_frame():
    for i in range(5):
        await sleep(1)
        print(i + 1)

loop = EventLoop()
loop.run(main_frame)

Here is an example of running two frames in parallel:

from asyncframes import Frame, sleep
from asyncframes.asyncio_eventloop import EventLoop

@Frame
async def counter(c):
    for i in range(5):
        await sleep(1)
        print(c)

@Frame
async def main_frame():
    a = counter('a') # Start counter 'a'
    await sleep(0.5) # Wait 0.5 seconds
    b = counter('b') # Start counter 'b'
    await (a & b) # Wait until both counters finish

loop = EventLoop()
loop.run(main_frame)

Here is an example of running two blocking operations in parallel using a parallel frame (PFrame):

import time
from asyncframes import Frame, PFrame, sleep
from asyncframes.asyncio_eventloop import EventLoop

@PFrame
async def counter(c):
    for i in range(5):
        time.sleep(1)
        print(c)

@Frame
async def main_frame():
    a = counter('a') # Start counter 'a'
    await sleep(0.5) # Wait 0.5 seconds
    b = counter('b') # Start counter 'b'
    await (a & b) # Wait until both counters finish

loop = EventLoop()
loop.run(main_frame)

Changelog

2.2.0 (2019-02-18)

  • Inline frames - Quickly create frame hierarchies using Python’s “with” syntax.

  • Lifebound awaitables - Create awaitables that fire when they are removed by passing lifebound=True.

  • Destructors - Define a destructor for any Awaitable or Primitive by overloading _ondispose().

2.1.0 (2019-01-07)

  • GTK support - Create GTK widgets using the GLib eventloop.

2.0.0 (2018-12-06)

  • Multithreading - Run frames in parallel using PFrame’s.

  • Delayed startup - By default creating a frame queues it’s execution and returns immediately.

  • Frame exception handlers - Exceptions propagate along the frame hierarchy, instead of along awaiting frames.

  • Simplified events - EventSource’s are now Event’s. Awaited events emit only event arguments.

  • Cancelable free events - Cancel free events by setting event.cancel to True.

  • Frame factories - Instances of frame classes are of type [MyFrameClass].Factory.

  • Threadsafe post() - Use post() to queue events on any thread, instead of separate post & invoke functions.

  • “singleshot” - Event argument “autoremove” has been renamed to “singleshot”

1.1.0 (2018-09-18)

  • Threadsafe events - Wake up event sources across threads using invoke().

  • Free event - Frames emit the self.free event just before they are removed.

  • Hierarchy changes - any_ and all_ do not take over parenthood of their awaitables.

1.0.0 (2018-09-05)

  • Initial release

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

asyncframes-2.2.0.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

asyncframes-2.2.0-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file asyncframes-2.2.0.tar.gz.

File metadata

  • Download URL: asyncframes-2.2.0.tar.gz
  • Upload date:
  • Size: 24.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0rc1

File hashes

Hashes for asyncframes-2.2.0.tar.gz
Algorithm Hash digest
SHA256 2a260052016fa2afd667adad68855e443e05903ac6f67d977f12c36fc2b92f73
MD5 25aa1e4f2d41698d988e88a4ccff336b
BLAKE2b-256 ad196538aba7a75d4441975b30f1f82c23f47034f943c547e274534cb7e3b683

See more details on using hashes here.

File details

Details for the file asyncframes-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: asyncframes-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0rc1

File hashes

Hashes for asyncframes-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd29f4572922e9ccbc47780d7ec7505cf12e50f9b0320a905603618455f274e5
MD5 33f373f93c22547fe88d16fbef4fce67
BLAKE2b-256 bef82009420996f91dc588d097f03bc6b9d931698e240c9f255566f8051b65ac

See more details on using hashes here.

Supported by

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