一个简单的多处理接口

mantichora的Python项目详细描述


pypi version><doibuild statuscodecov

曼蒂科拉

一个简单的多处理界面


mantichora提供了一个简单的接口 多处理

frommantichoraimportmantichorawithmantichora()asmcore:mcore.run(func1)mcore.run(func2)mcore.run(func3)mcore.run(func4)results=mcore.returns()
 100.00% :::::::::::::::::::::::::::::::::::::::: |    12559 /    12559 |:  func1
  71.27% ::::::::::::::::::::::::::::             |    28094 /    39421 |:  func2
  30.34% ::::::::::::                             |    28084 /    92558 |:  func3
  35.26% ::::::::::::::                           |    27282 /    77375 |:  func4

您可以简单地给mantichora提供运行所需的任意多个函数。 mantichora将使用 并给你函数的返回值。这个 返回值按函数的顺序排序 最初是给曼蒂科拉的。进度条来自 atpbar可用于 功能。

这个包中的代码最初是在子包中开发的 并发alphatwirl

此文件中的示例也可以在jupyter笔记本上运行。< BR>binder



要求

  • python 2.7、3.6或3.7

安装

您可以使用conda forge中的conda安装:

conda install -c conda-forge mantichora

或使用pip

pip install -U mantichora

用户指南

快速启动

我将在这里通过简单的例子演示如何使用mantichora。

导入库

我们将使用两个python标准库 时间和 在一个例子中随机。 任务函数。在示例任务函数中,我们还将使用 对于进度条,请使用atpbar。 导入这些包并mantichora

importtime,randomfromatpbarimportatpbarfrommantichoraimportmantichora

定义任务函数

让我们定义一个简单的任务函数。

deftask_loop(name,ret=None):n=random.randint(1000,10000)foriinatpbar(range(n),name=name):time.sleep(0.0001)returnret

此函数的任务是睡眠0.0001秒 从100010000atpbar用于显示进度条。函数接受 两个参数:name,进度条上的标签,以及 函数的返回值。

注意:曼蒂科拉使用 多处理 在后台进程中运行任务函数。因此,任务 函数、它们的参数和返回值必须是 可pickle

您只需尝试在不使用mantichora的情况下运行此函数即可。

result=task_loop('task1','result1')

这不会立即返回。它等待函数完成。 您将看到一个进度条。

 100.00% :::::::::::::::::::::::::::::::::::::::: |    58117 /    58117 |:  task1

返回值存储在result

print(result)
frommantichoraimportmantichorawithmantichora()asmcore:mcore.run(func1)mcore.run(func2)mcore.run(func3)mcore.run(func4)results=mcore.returns()
0

与mantichora同时运行任务

现在,我们与mantichora同时运行多个任务。

frommantichoraimportmantichorawithmantichora()asmcore:mcore.run(func1)mcore.run(func2)mcore.run(func3)mcore.run(func4)results=mcore.returns()
1

在上面的示例代码中,mantichora用 可选参数nworkersnWorkers指定 工人们。在上面的例子中是3。默认值为4。AT 大多数任务(如nWorkers任务)都可以并发运行。

with语句" rel="nofollow">with 声明 在示例中使用。这样可以确保mantichora正确 结束工作。

您可以将任务函数及其参数赋给mcore.run()。你 可以根据需要多次调用mcore.run()。在上面 例如,mcore.run()使用与 不同的论点。每次也可以使用不同的函数。 mcore.run()立即返回;它不会等待任务 完成甚至开始。在每个调用中,mcore.run()只放置一个任务 在队列中后台进程中的工作人员从 排队并运行。

mcore.returns()等待所有任务完成并返回 返回值,按任务的顺序排序 最初给mcore.run()

进度条将由atpbar显示。

frommantichoraimportmantichorawithmantichora()asmcore:mcore.run(func1)mcore.run(func2)mcore.run(func3)mcore.run(func4)results=mcore.returns()
2

不管顺序如何,结果都按原始顺序排序 在其中,任务已完成。

frommantichoraimportmantichorawithmantichora()asmcore:mcore.run(func1)mcore.run(func2)mcore.run(func3)mcore.run(func4)results=mcore.returns()
3
frommantichoraimportmantichorawithmantichora()asmcore:mcore.run(func1)mcore.run(func2)mcore.run(func3)mcore.run(func4)results=mcore.returns()
4

功能

不带语句
end()

如果不将语句一起使用,则需要调用end()

frommantichoraimportmantichorawithmantichora()asmcore:mcore.run(func1)mcore.run(func2)mcore.run(func3)mcore.run(func4)results=mcore.returns()
5
frommantichoraimportmantichorawithmantichora()asmcore:mcore.run(func1)mcore.run(func2)mcore.run(func3)mcore.run(func4)results=mcore.returns()
6
terminate()

mantichora可以用terminate()终止。在terminate()之后 调用时,end()仍需要调用。在下面的例子中, terminate()在某些任务睡眠0.5秒后调用 仍在运行。

frommantichoraimportmantichorawithmantichora()asmcore:mcore.run(func1)mcore.run(func2)mcore.run(func3)mcore.run(func4)results=mcore.returns()
7

当任务终止时,进度条停止。

frommantichoraimportmantichorawithmantichora()asmcore:mcore.run(func1)mcore.run(func2)mcore.run(func3)mcore.run(func4)results=mcore.returns()
8

任务完成时接收结果

而不是在收到 鲁尔斯,你可以在任务完成后用这个方法得到结果 receive_one()receive_receive()

接收一个()

方法receive_one()返回一对运行id和 任务函数的值。如果没有任务完成,receive_one() 等待一个任务完成。receive_one()返回无 任务尚未完成。方法返回 任务:

frommantichoraimportmantichorawithmantichora()asmcore:mcore.run(func1)mcore.run(func2)mcore.run(func3)mcore.run(func4)results=mcore.returns()
9
 100.00% :::::::::::::::::::::::::::::::::::::::: |    12559 /    12559 |:  func1
  71.27% ::::::::::::::::::::::::::::             |    28094 /    39421 |:  func2
  30.34% ::::::::::::                             |    28084 /    92558 |:  func3
  35.26% ::::::::::::::                           |    27282 /    77375 |:  func4
0

runid是按以下任务顺序排列的运行id列表 已被赋予run()

 100.00% :::::::::::::::::::::::::::::::::::::::: |    12559 /    12559 |:  func1
  71.27% ::::::::::::::::::::::::::::             |    28094 /    39421 |:  func2
  30.34% ::::::::::::                             |    28084 /    92558 |:  func3
  35.26% ::::::::::::::                           |    27282 /    77375 |:  func4
1
 100.00% :::::::::::::::::::::::::::::::::::::::: |    12559 /    12559 |:  func1
  71.27% ::::::::::::::::::::::::::::             |    28094 /    39421 |:  func2
  30.34% ::::::::::::                             |    28084 /    92558 |:  func3
  35.26% ::::::::::::::                           |    27282 /    77375 |:  func4
2

按任务完成的顺序排列。

 100.00% :::::::::::::::::::::::::::::::::::::::: |    12559 /    12559 |:  func1
  71.27% ::::::::::::::::::::::::::::             |    28094 /    39421 |:  func2
  30.34% ::::::::::::                             |    28084 /    92558 |:  func3
  35.26% ::::::::::::::                           |    27282 /    77375 |:  func4
3
 100.00% :::::::::::::::::::::::::::::::::::::::: |    12559 /    12559 |:  func1
  71.27% ::::::::::::::::::::::::::::             |    28094 /    39421 |:  func2
  30.34% ::::::::::::                             |    28084 /    92558 |:  func3
  35.26% ::::::::::::::                           |    27282 /    77375 |:  func4
4
接收完成()

方法receive_finished()返回运行ID对的列表 以及已完成任务函数的返回值。方法 receive_finished()不等待任务完成。它返回一个 如果没有任务完成,则为空列表。

 100.00% :::::::::::::::::::::::::::::::::::::::: |    12559 /    12559 |:  func1
  71.27% ::::::::::::::::::::::::::::             |    28094 /    39421 |:  func2
  30.34% ::::::::::::                             |    28084 /    92558 |:  func3
  35.26% ::::::::::::::                           |    27282 /    77375 |:  func4
5
 100.00% :::::::::::::::::::::::::::::::::::::::: |    12559 /    12559 |:  func1
  71.27% ::::::::::::::::::::::::::::             |    28094 /    39421 |:  func2
  30.34% ::::::::::::                             |    28084 /    92558 |:  func3
  35.26% ::::::::::::::                           |    27282 /    77375 |:  func4
6

runid再次是按任务顺序排列的运行id列表 给了run()

 100.00% :::::::::::::::::::::::::::::::::::::::: |    12559 /    12559 |:  func1
  71.27% ::::::::::::::::::::::::::::             |    28094 /    39421 |:  func2
  30.34% ::::::::::::                             |    28084 /    92558 |:  func3
  35.26% ::::::::::::::                           |    27282 /    77375 |:  func4
1
 100.00% :::::::::::::::::::::::::::::::::::::::: |    12559 /    12559 |:  func1
  71.27% ::::::::::::::::::::::::::::             |    28094 /    39421 |:  func2
  30.34% ::::::::::::                             |    28084 /    92558 |:  func3
  35.26% ::::::::::::::                           |    27282 /    77375 |:  func4
2

也按任务完成的顺序再次出现。

 100.00% :::::::::::::::::::::::::::::::::::::::: |    12559 /    12559 |:  func1
  71.27% ::::::::::::::::::::::::::::             |    28094 /    39421 |:  func2
  30.34% ::::::::::::                             |    28084 /    92558 |:  func3
  35.26% ::::::::::::::                           |    27282 /    77375 |:  func4
3
conda install -c conda-forge mantichora
0

日志记录

登录后台进程将传播到 日志记录的a部分描述的方法 食谱

下面是一个简单的示例任务函数,它使用日志记录。任务 函数在返回前进行日志记录。

conda install -c conda-forge mantichora
1

将日志记录流设置为字符串流,以便我们以后可以 以字符串形式检索日志。

conda install -c conda-forge mantichora
2

运行任务。

conda install -c conda-forge mantichora
3
conda install -c conda-forge mantichora
4

在后台进程中的任务函数中进行的日志记录被发送到 在字符串流中写入的主进程。

conda install -c conda-forge mantichora
5
conda install -c conda-forge mantichora
6

许可证

  • Mantichora根据BSD许可证获得许可。

联系人

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

推荐PyPI第三方库


热门话题
java无法在未设置数据源的情况下启动springboot应用程序   返回/泛型的类型?   java通过在navigationView中按id重新加载navigationView内容   java实现安卓的状态更新   java Equals()对于两个相等的字符串不返回true   java如何保存屏幕截图(matlab)   java GWT如何在重新加载页面之前确保函数已完全执行   java在Groovy中实现ObjectJSON映射的标准方法是什么?   java在ApacheTomcat中,是否可以通过连接器过滤多个访问日志文件?   java当JVM达到其Xmx限制时,它会强制垃圾收集吗?   如何在JAVA中生成包含特定数字的不同随机数列表?   rcp中透视图之间的java切换   java理解名为“分区”的Linkedlist算法中的无限循环   RestTemplate的java测微计统计信息   Android中使用自定义服务BLE的java读/写特性   java验证输入以确保负数   关于Java扫描器的io基本查询   java如何使用子字符串或其他函数将字符串拆分为单词?   java Storm群集重复元组