一个简单的多处理接口
mantichora的Python项目详细描述
曼蒂科拉
一个简单的多处理界面
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>
要求
- 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
秒
从1000
和
10000
。atpbar
用于显示进度条。函数接受
两个参数: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
用
可选参数nworkers
。nWorkers
指定
工人们。在上面的例子中是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许可证获得许可。
联系人
推荐PyPI第三方库
语句
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许可证获得许可。
联系人
推荐PyPI第三方库