一个用于同步和异步依赖图计算的包
aiocells的Python项目详细描述
aiocells
是一个为同步和异步提供工具的包
在依赖关系图中执行节点。在
内容:
示例
你好世界
这是first demo的代码。在
#!/usr/bin/env python3importaiocellsdefhello_world():print("Hello, world!")defmain():graph=aiocells.DependencyGraph()# The node can be any callable, in this case a function.graph.add_node(hello_world)aiocells.compute_sequential(graph)
这是synchronous图计算。图中只有一个节点。
它是一个打印消息的函数。同步节点必须是callable
。在
定义排序约束
这是demo 4。它显示了边之间的节点 定义如下:
^{pr2}$在本例中,有三个节点。在添加节点之后,我们定义 它们之间的优先关系。当计算图形时,它是 以尊重优先关系的方式这样做。在
异步节点
下面是demo_5的代码。注意
asyncio.sleep
、functools.partial
和{
#!/usr/bin/env python3importasynciofromfunctoolsimportpartialimportaiocells# This example demonstrates graph nodes that are coroutines. We use# a different computer; one that know how to deal with coroutines.defmain():graph=aiocells.DependencyGraph()# First, we add a lambda functionbefore_sleep=graph.add_node(lambda:print("Sleeping..."))# Second, we create a coroutine function using functools.partial. This# is the closest we can get to a lambda for an async functionsleep_2=partial(asyncio.sleep,2)# Finally, another lambda functionwake_up=graph.add_node(lambda:print("Woke up!"))# Here, 'sleep' will implicitly be added to the graph because it is# part of the precedence relationshipgraph.add_precedence(before_sleep,sleep_2)graph.add_precedence(sleep_2,wake_up)# Here, we use the `async_compute_sequential`, which, like# `compute_sequential`, call the nodes in a topologically correct sequence.# However, whereas `compute_sequential` only supports vanilla callables,# `async_compute_sequential` additionally supports coroutine functions,# as defined by `inspect.iscoroutinefunction`. However, the execution is# still sequential. Each coroutine function is executed using 'await' and# must complete before the next node is executed. The function# `async_compute_sequential` is a coroutine and must be awaited. Here,# we simply pass it to `asyncio.run`.asyncio.run(aiocells.async_compute_sequential(graph))
并发计算
demo 6是可以的一个图形示例
同时计算,但不是由于使用if async_compute_sequential
。在
importasynciofromfunctoolsimportpartialimportaiocellsdefcreate_graph(stopwatch):graph=aiocells.DependencyGraph()# The method to start the stopwatchstart_stopwatch=stopwatch.start# Two sleeps. Note that they are asyncio.sleepsleep_1=partial(asyncio.sleep,1)sleep_2=partial(asyncio.sleep,2)# The method to stop the stopwatchstop_stopwatch=stopwatch.stop# Start the stopwatch before the first sleepgraph.add_precedence(start_stopwatch,sleep_1)# Stop the stopwatch after the first sleepgraph.add_precedence(sleep_1,stop_stopwatch)# Start the stopwatch before the second sleepgraph.add_precedence(start_stopwatch,sleep_2)# Stop the stopwatch after the second sleepgraph.add_precedence(sleep_2,stop_stopwatch)# Note that there is no precedence relationship between the two# sleeps.returngraphdefmain():stopwatch=aiocells.Stopwatch()graph=create_graph(stopwatch)# Even though the graph is a diamond (the sleeps do no depend on each# other and _could_ be executed concurrenty, `async_compute_sequential`# does not support concurrent execution. Thus, the execution time is# about 3 seconds, the sum of the two sleeps.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是与上面相同的图,但是是计算出来的
与async_compute_concurrent
同时进行。在
#!/usr/bin/env python3importasyncioimportaiocellsimportaiocells.demo_6asdemo_6defmain():stopwatch=aiocells.Stopwatch()graph=demo_6.create_graph(stopwatch)# Here, we run the same graph as the previous demo but we use# 'async_compute_concurrent' which will run the two sleeps concurrently.# Thus, the execution time will be around 2 seconds, the maximum of# the two sleeps.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()}")
开发安装
存储库中有一个Makefile
。默认目标将初始化
一个虚拟环境,将依赖项安装到该环境中,然后
测试代码。它要求Python 3.8
、virtualenv
和{
$ make
激活虚拟环境并运行演示
默认make目标将生成一个名为activate_aiocells
的文件。到
激活虚拟环境:
$ source activate_aiocells
完成此操作后,应该可以使用以下命令:
$ aiocells demo-1
制表位完成
activate_aiocells
将启用aiocells
的制表符完成:
$ aiocells <TAB>
可编辑安装
将使用在虚拟环境中安装包
pip --editable
。这意味着对代码的修改将立即
可用。在
要测试这一点,请尝试修改src/aiocells/demo_1.py
以打印不同的
留言。你应该能够立即运行演示,并看到新的
信息:
$ aiocells demo-1
- 项目
标签: