从函数的多处理和大数据集的解析中省去了麻烦。
processio的Python项目详细描述
Python进程
无需多处理即可运行函数
{1}$
一个很好的包,可以帮助您运行多处理函数并获得结果。
安装
pip install processio
用法
在自己的进程中运行函数
^{pr2}$主命令
# Import the modulefromprocessioimportProcessIO# Start your function with or without argumentsvar=ProcessIO(function,args,kwargs)# Wait for the function to finnish or get the result if its finishedvar.result()
可选命令
# Check if your thread is still working on the function.# This will return True if the function is not completed.var.doing_work()
使用parseIO可以节省列表解析的大量时间
importtimefromprocessioimportParseIOdeflist_parser(list):result=0forlineinlist:result+=get_total_amout(line)returnresultdefdo_some_work():# this will split the list into 4 and run the function on# 4 different processes, that in most cases will almost speed# up the work time by 4.# you can define the number of processes you want to run, but as a# default the module runs on the systems cpu cores - 1parser=ParseIO(list_parser,huge_list)# do stuff or run a while loop to wait for resultwhileparser.doing_work():print('Waiting for processes to finnish')# You can also call .result() and the main thread will wait # for the thread to return your result.result=parser.result()# result comes back as list pr process, so in this case we will get # a list with 4 numbers that we can loop thru.print(result)# Outputs -> [1000, 1000, 1000, 1000] <- exampletotal=0forresinresult:total+=resprint(total)# Outputs -> 4000 <- exampleif__name__=='__main__':# <- its required to execute the function maindo_some_work()
测试
使用以下命令运行测试。在
python -m unittest threadit.tests.test_threadit
变更日志:
看到了吗变更日志.md在
- 项目
标签: