File: map_pool.py

package info (click to toggle)
pathos 0.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 812 kB
  • sloc: python: 4,506; sh: 38; makefile: 33
file content (34 lines) | stat: -rw-r--r-- 1,198 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python
#
# Author: Mike McKerns (mmckerns @uqfoundation)
# Copyright (c) 2024-2026 The Uncertainty Quantification Foundation.
# License: 3-clause BSD.  The full license text is available at:
#  - https://github.com/uqfoundation/pathos/blob/master/LICENSE

from pathos.maps import Imap
from pathos.pools import ProcessPool
squared = lambda x:x*x

# serial map (in-line for loop)
print("list(map(squared, range(4))): %s" % list(map(squared, range(4))))

# pathos serial map
_map = Imap()
print("list(Imap()(squared, range(4))): %s" % list(_map(squared, range(4))))

# pathos process-parallel map
_map = Imap(ProcessPool)
print("list(Imap(ProcessPool)(squared, range(4))): %s" % list(_map(squared, range(4))))

# pathos pool-based parallel map
pool = ProcessPool()
print("list(ProcessPool().imap(squared, range(4))): %s" % list(pool.imap(squared, range(4))))

# pathos asynchronous parallel map
result = pool.amap(squared, range(4))
print("ProcessPool().amap(squared, range(4)).get(): %s" % result.get())

# pathos thread-parallel map
from pathos.pools import ThreadPool
tpool = ThreadPool()
print("list(ThreadPool().imap(squared, range(4))): %s" % list(tpool.imap(squared, range(4))))