python之map函数

今天在内网里看到有人讨论python里map函数的运行方式,正好找了点资料自己实践了下,这里做个总结。

内建的map函数

1
2
3
4
5
6
7
8
9
10
import time
from datetime import datetime

def add(x,y):
print datetime.now(), "start.."
time.sleep(5)
print datetime.now(), "end.."
return x + y

map(add,[1, 2, 3],[4, 5, 6])

返回的结果:

2019-07-09 23:08:32.748735 start..
2019-07-09 23:08:37.752953 end..
2019-07-09 23:08:37.753019 start..
2019-07-09 23:08:42.755606 end..
2019-07-09 23:08:42.755672 start..
2019-07-09 23:08:47.757787 end..

可以看出来运行的时候是串行执行的,按照顺序进行执行,如果想并行的方式运行map,则可以用以下方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import time
from datetime import datetime
from multiprocessing.dummy import Pool as ThreadPool
from functools import partial

def add(x,y):
print datetime.now(), "start.."
time.sleep(5)
print datetime.now(), "end.."
return x + y

#map(add,[1, 2, 3],[4, 5, 6])

def add_wrap(args):
return add(*args)

if __name__ == "__main__":
pool = ThreadPool(4)
print pool.map(add_wrap, [(1,2), (3,4), (5,6)])
pool.close()
pool.join()

返回的结果:

2019-07-09 23:16:14.896814 start..
2019-07-09 23:16:14.896944 start..
2019-07-09 23:16:14.897000 start..
2019-07-09 23:16:19.898997 2019-07-09 23:16:19.899071 end..
end..2019-07-09 23:16:19.899232 end..

参考链接