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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Radek Hušek
group-connectivity-pub
Commits
62d5e6f1
Commit
62d5e6f1
authored
9 years ago
by
Radek Hušek
Committed by
Radek Hušek
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
parmap: add multimap & out_chunksize options
parent
c6046692
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
+29
-7
29 additions, 7 deletions
parmap.py
with
29 additions
and
7 deletions
parmap.py
+
29
−
7
View file @
62d5e6f1
#
modified version of
code written by klaus se from
#
based on
code written by klaus se from
# http://stackoverflow.com/a/16071616
# but now almost completly rewritten
import
multiprocessing
from
itertools
import
chain
...
...
@@ -22,19 +23,36 @@ def producer_fun(X, q_in, cont, nprocs, chunksize):
for
_
in
range
(
nprocs
):
q_in
.
put
((
None
,
None
))
def
worker_fun
(
f
,
q_in
,
q_out
):
def
worker_fun
(
f
,
q_in
,
q_out
,
multimap
,
chunksize
):
while
True
:
i
,
chunk
=
q_in
.
get
()
if
i
is
None
:
q_out
.
put
((
None
,
None
))
break
q_out
.
put
((
i
,
[
f
(
x
)
for
x
in
chunk
]))
ret
=
(
f
(
x
)
for
x
in
chunk
)
if
multimap
:
ret
=
chain
.
from_iterable
(
ret
)
if
chunksize
is
not
None
:
for
x
in
chunkme
(
ret
,
chunksize
):
q_out
.
put
((
i
,
x
))
q_out
.
put
((
i
,
None
))
else
:
q_out
.
put
((
i
,
list
(
ret
)))
def
parmap
(
f
,
X
,
nprocs
=
None
,
chunksize
=
1
,
chunks_in_flight
=
None
,
inOrder
=
True
):
inOrder
=
True
,
multimap
=
False
,
out_chunksize
=
None
):
if
nprocs
is
None
:
nprocs
=
multiprocessing
.
cpu_count
()
if
inOrder
:
out_chunksize
=
None
if
out_chunksize
is
not
None
:
out_chunksize
=
max
(
out_chunksize
,
1
)
if
chunks_in_flight
is
None
:
chunks_in_flight
=
10
+
3
*
nprocs
...
...
@@ -45,7 +63,8 @@ def parmap(f, X, nprocs = None, chunksize = 1, chunks_in_flight = None,
q_out
=
multiprocessing
.
Queue
()
proc
=
[
multiprocessing
.
Process
(
target
=
worker_fun
,
args
=
(
f
,
q_in
,
q_out
)
target
=
worker_fun
,
args
=
(
f
,
q_in
,
q_out
,
multimap
,
out_chunksize
)
)
for
_
in
range
(
nprocs
)]
proc
.
append
(
multiprocessing
.
Process
(
...
...
@@ -69,8 +88,11 @@ def parmap(f, X, nprocs = None, chunksize = 1, chunks_in_flight = None,
continue
if
not
inOrder
:
chunk_index
+=
1
if
out_chunksize
is
None
:
cont
.
release
()
if
val
is
None
:
cont
.
release
()
continue
yield
val
continue
...
...
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