aiocells
is a package that provides tools for synchronous and asynchronous
execution of nodes in a dependency graph.
Contents:
- Examples
- Development Installation
Examples
Hello world
Here is the code for the first demo.
import aiocells
def hello_world():
print("Hello, world!")
def main():
graph = aiocells.DependencyGraph()
graph.add_node(hello_world)
aiocells.compute_sequential(graph)
This is synchronous graph computation. There is only one node in the graph.
It is a function that prints a message. Synchronous nodes must be callable
.
Defining ordering constraints
Here is demo 4. It shows how edges between nodes
are defined:
import time
import aiocells
def main():
graph = aiocells.DependencyGraph()
print_sleeping = graph.add_node(lambda: print("Sleeping..."))
sleep = graph.add_node(lambda: time.sleep(2))
print_woke_up = graph.add_node(lambda: print("Woke up!"))
print("Define the precedence relationships...")
graph.add_precedence(print_sleeping, sleep)
graph.add_precedence(sleep, print_woke_up)
aiocells.compute_sequential(graph)
In this case, there are three nodes. After the nodes are added, we define
precedence relationships between them. When the graph is computed, it is
done so in a way that honours the precedence relationships.
Asynchronous nodes
Below is the code for demo_5. Note the use of
asyncio.sleep
, functools.partial
and aiocells.async_compute_sequential
.
import asyncio
from functools import partial
import aiocells
def main():
graph = aiocells.DependencyGraph()
before_sleep = graph.add_node(lambda: print("Sleeping..."))
sleep_2 = partial(asyncio.sleep, 2)
wake_up = graph.add_node(lambda: print("Woke up!"))
graph.add_precedence(before_sleep, sleep_2)
graph.add_precedence(sleep_2, wake_up)
asyncio.run(aiocells.async_compute_sequential(graph))
Concurrent computation
demo 6 is a an example of graph that could be
computed concurrently but is not due to the use if async_compute_sequential
.
import asyncio
from functools import partial
import aiocells
def create_graph(stopwatch):
graph = aiocells.DependencyGraph()
start_stopwatch = stopwatch.start
sleep_1 = partial(asyncio.sleep, 1)
sleep_2 = partial(asyncio.sleep, 2)
stop_stopwatch = stopwatch.stop
graph.add_precedence(start_stopwatch, sleep_1)
graph.add_precedence(sleep_1, stop_stopwatch)
graph.add_precedence(start_stopwatch, sleep_2)
graph.add_precedence(sleep_2, stop_stopwatch)
return graph
def main():
stopwatch = aiocells.Stopwatch()
graph = create_graph(stopwatch)
print("Two async sleeps computed sequentially.")
print("Total time should take about 3 seconds...")
asyncio.run(aiocells.async_compute_sequential(graph))
print("Computation with `async_compute_sequential` took"
f" {stopwatch.elapsed_time()}")
demo_7 is the same graph as above but computed
concurrently with async_compute_concurrent
.
import asyncio
import aiocells
import aiocells.demo_6 as demo_6
def main():
stopwatch = aiocells.Stopwatch()
graph = demo_6.create_graph(stopwatch)
print("Running previous demo's graph concurrently.")
print("Total execution time should be about 2 seconds...")
asyncio.run(aiocells.async_compute_concurrent(graph))
print("Computation with `async_compute_concurrent` took"
f" {stopwatch.elapsed_time()}")
Development Installation
There is a Makefile
in the repository. The default target will initialise
a virtual environment, install dependencies into that environment and then
test the code. It requires Python 3.8
, virtualenv
and pip
to be
installed. If those are missing, it will print suggestions on how to address
the problem.
$ make
Activating the virtual environment and running the demos
The default make target will generate a file called activate_aiocells
. To
activate the virtual environment:
$ source activate_aiocells
Once you've done that, you should have the following command available:
$ aiocells demo-1
Tab completion
activate_aiocells
will enable tab completion for aiocells
:
$ aiocells <TAB>
Editable installation
The package will be installed in the virutal environment using
pip --editable
. This means that modifications to the code will be immediately
available.
To test this, try modifying src/aiocells/demo_1.py
to print a different
message. You should be able to immediately run the demo and see the new
message:
$ aiocells demo-1