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

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中将字符串转换为音频输入   java如何使用Spring Boot RestTemplate解组json列表   计算机上C++ OpenCV项目转换为OpenCV Android的java步骤   Java Spring Telegraf数据表不工作   openurlconnection与connect之间的java差异?   java Solr字段搜索、通配符和转义字符   java如何使用DLFolderLocalServiceUtil类的getFolders()方法?   尝试用Java编写文件   java连续双缓冲解决方案不起作用   两个数组的Java乘积   java Jackson,如何正确编写自定义反序列化程序   java将布尔标志按一定顺序排列以获得更好的性能是否有意义   java Vaadin无缓冲网格无法关闭   java在MySQL中以同一用户身份同时从不同客户端登录安全吗?   java如何使用安卓asynchttp库   java无法在Android中使用AlarmManager停止服务   java在Hibernate的XML配置文件中指定默认值   Spring启动应用程序中的java内存SQLITE不工作   Java代码无法写入文本文件,