python并行处理任务,在python中并行执行任务

I am using python 2.7, I have some code that looks like this:

task1()

task2()

task3()

dependent1()

task4()

task5()

task6()

dependent2()

dependent3()

The only dependencies here are as follows: dependent1 needs to wait for tasks1-3, dependent2 needs to wait for tasks 4-6 and dependent3 needs to wait for dependents1-2... The following would be okay: running the whole 6 tasks first in parallel, then the first two dependents in parallel.. then the final dependent

I prefer to have as much tasks as possible running in parallel, I've googled for some modules but I was hoping to avoid external libraries, and not sure how the Queue-Thread technique can solve my problem (maybe someone can recommend a good resource?)

解决方案

The builtin threading.Thread class offers all you need: start to start a new thread and join to wait for the end of a thread.

import threading

def task1():

pass

def task2():

pass

def task3():

pass

def task4():

pass

def task5():

pass

def task6():

pass

def dep1():

t1 = threading.Thread(target=task1)

t2 = threading.Thread(target=task2)

t3 = threading.Thread(target=task3)

t1.start()

t2.start()

t3.start()

t1.join()

t2.join()

t3.join()

def dep2():

t4 = threading.Thread(target=task4)

t5 = threading.Thread(target=task5)

t4.start()

t5.start()

t4.join()

t5.join()

def dep3():

d1 = threading.Thread(target=dep1)

d2 = threading.Thread(target=dep2)

d1.start()

d2.start()

d1.join()

d2.join()

d3 = threading.Thread(target=dep3)

d3.start()

d3.join()

Alternatively to join you can use Queue.join to wait for the threads end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值