Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
group-connectivity-pub
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Radek Hušek
group-connectivity-pub
Commits
b2d95f55
Commit
b2d95f55
authored
9 years ago
by
Radek Hušek
Committed by
Radek Hušek
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
parmap.py: add doc strings
parent
91f2d78d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
parmap.py
+19
-0
19 additions, 0 deletions
parmap.py
with
19 additions
and
0 deletions
parmap.py
+
19
−
0
View file @
b2d95f55
...
@@ -6,6 +6,7 @@ import multiprocessing
...
@@ -6,6 +6,7 @@ import multiprocessing
from
itertools
import
chain
from
itertools
import
chain
def
chunkme
(
X
,
chunksize
):
def
chunkme
(
X
,
chunksize
):
"""
Return items generated by X in chunks (lists) of size chunksize.
"""
chunk
=
[]
chunk
=
[]
for
x
in
X
:
for
x
in
X
:
chunk
.
append
(
x
)
chunk
.
append
(
x
)
...
@@ -16,6 +17,7 @@ def chunkme(X, chunksize):
...
@@ -16,6 +17,7 @@ def chunkme(X, chunksize):
yield
chunk
yield
chunk
def
producer_fun
(
X
,
q_in
,
cont
,
nprocs
,
chunksize
):
def
producer_fun
(
X
,
q_in
,
cont
,
nprocs
,
chunksize
):
"""
parmap internal helper
"""
for
i
,
x
in
enumerate
(
chunkme
(
X
,
chunksize
)):
for
i
,
x
in
enumerate
(
chunkme
(
X
,
chunksize
)):
cont
.
acquire
()
cont
.
acquire
()
q_in
.
put
((
i
,
x
))
q_in
.
put
((
i
,
x
))
...
@@ -24,6 +26,7 @@ def producer_fun(X, q_in, cont, nprocs, chunksize):
...
@@ -24,6 +26,7 @@ def producer_fun(X, q_in, cont, nprocs, chunksize):
q_in
.
put
((
None
,
None
))
q_in
.
put
((
None
,
None
))
def
worker_fun
(
f
,
q_in
,
q_out
,
multimap
,
chunksize
):
def
worker_fun
(
f
,
q_in
,
q_out
,
multimap
,
chunksize
):
"""
parmap internal helper
"""
while
True
:
while
True
:
i
,
chunk
=
q_in
.
get
()
i
,
chunk
=
q_in
.
get
()
if
i
is
None
:
if
i
is
None
:
...
@@ -44,6 +47,22 @@ def worker_fun(f, q_in, q_out, multimap, chunksize):
...
@@ -44,6 +47,22 @@ def worker_fun(f, q_in, q_out, multimap, chunksize):
def
parmap
(
f
,
X
,
nprocs
=
None
,
chunksize
=
1
,
chunks_in_flight
=
None
,
def
parmap
(
f
,
X
,
nprocs
=
None
,
chunksize
=
1
,
chunks_in_flight
=
None
,
in_order
=
True
,
multimap
=
False
,
out_chunksize
=
None
):
in_order
=
True
,
multimap
=
False
,
out_chunksize
=
None
):
"""
Implements parallel version of map.
Spawns producer and (multiple) worker threads. Works as generator
calculating new values as needed (see below for fine tuning of ahead
computation). Prevents memory exhaustion in case of fast workers
and slow consumption of generated values.
f -- function to map on each element of X
X -- generator
nprocs -- number of worker threads to spawn
chunksize -- number of items retrieved by worker from queue at once
chunks_in_flight -- maximal number of chunks being processed at once
in_order -- if True, input sequence may be reordered during processing
multimap -- if True, f is expected to return an Iterable and flattening is done
out_chunksize -- size of chunks send from workers to consumer
"""
if
nprocs
is
None
:
if
nprocs
is
None
:
nprocs
=
multiprocessing
.
cpu_count
()
nprocs
=
multiprocessing
.
cpu_count
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment