一个用于同步和异步依赖图计算的包

aiocells的Python项目详细描述


aiocells是一个为同步和异步提供工具的包 在依赖关系图中执行节点。在

内容:

  1. Examples
  2. Development Installation

示例

你好世界

这是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.sleepfunctools.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.8virtualenv和{}是 安装。如果这些都不见了,它会打印关于如何解决的建议 问题。在

$ 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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java需要一种简单的方法来创建用于排序的comparator类   java getReadableDatabase和getWriteableDatabase无法解析   查找列表<Long>中与某个元素对应的所有索引的java方法   安卓将视图添加到ViewFlipper会导致java。StackOverflowerr语言   java根据它所包含的长“curTime”类字段将N的列表拆分为24(小时)   Android N中的Java8流API   自动生成Java策略文件的安全性   垃圾收集鼓励Java中的主要GC(但不是STW GC)   java如何检查UDP服务器上侦听的客户端数量   在前一台主机被Datastax Java驱动程序关闭后,Cassandra尝试重新连接到下一台主机   java如何使用Spring Boot创建部分代理   java是否有一个网站或资源可以完全比较EJB版本   java无需使用第三方库从gradle生成输出   继承由于这个多态性的基本示例中的语法有什么不同吗?(爪哇)   java字符串数组中的空字符串   java为什么CMS中的初始标记阶段是串行的   为什么Lucene有时与InChIKeys不匹配?   安卓通知Java应用程序数据库中的更改   java如何将单个json对象值解析为按钮   java打印堆栈将运行时错误跟踪到文件