Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pyParallelRunAndWrite
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
tetras-libre
pyParallelRunAndWrite
Commits
8526d3e6
Verified
Commit
8526d3e6
authored
6 years ago
by
David Beniamine
Browse files
Options
Downloads
Patches
Plain Diff
Small refactor
parent
bbf400ce
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
parallelRunAndWrite.py
+13
-17
13 additions, 17 deletions
parallelRunAndWrite.py
with
13 additions
and
17 deletions
parallelRunAndWrite.py
+
13
−
17
View file @
8526d3e6
...
@@ -46,20 +46,14 @@ def parallelRunAndWrite(func, tasks, outFileName, bar=True, errFileName=None, nu
...
@@ -46,20 +46,14 @@ def parallelRunAndWrite(func, tasks, outFileName, bar=True, errFileName=None, nu
with
mp
.
Pool
(
processes
=
numProc
)
as
pool
:
with
mp
.
Pool
(
processes
=
numProc
)
as
pool
:
# Define imap function
# Define imap function
if
(
ordered
):
imap_func
=
pool
.
imap_unordered
if
(
not
ordered
)
else
pool
.
imap
imap_func
=
pool
.
imap
else
:
imap_func
=
pool
.
imap_unordered
with
ExitStack
()
as
stack
:
with
ExitStack
()
as
stack
:
# open files
# open files
out
=
stack
.
enter_context
(
open
(
outFileName
,
'
w+
'
))
out
=
stack
.
enter_context
(
open
(
outFileName
,
'
w+
'
))
if
(
errFileName
):
err
=
stack
.
enter_context
(
open
(
errFileName
,
'
w+
'
))
if
(
errFileName
)
else
None
err
=
stack
.
enter_context
(
open
(
errFileName
,
'
w+
'
))
numErrors
=
0
else
:
err
=
None
numErrors
=
0
start
=
time
()
start
=
time
()
# actually do stuff
# actually do stuff
for
result
in
imap_func
(
worker
,
[(
func
,
t
,
err
is
not
None
)
for
t
in
tasks
],
chunksize
):
for
result
in
imap_func
(
worker
,
[(
func
,
t
,
err
is
not
None
)
for
t
in
tasks
],
chunksize
):
...
@@ -77,10 +71,7 @@ def parallelRunAndWrite(func, tasks, outFileName, bar=True, errFileName=None, nu
...
@@ -77,10 +71,7 @@ def parallelRunAndWrite(func, tasks, outFileName, bar=True, errFileName=None, nu
progressBar
.
update
()
progressBar
.
update
()
# Report status
# Report status
if
(
err
):
errmsg
=
'
{} errors / {} tasks written to file
"
{}
"
\n
'
.
format
(
numErrors
,
len
(
tasks
),
errFileName
)
if
(
errFileName
)
else
''
errmsg
=
'
{} errors / {} tasks written to file
"
{}
"
\n
'
.
format
(
numErrors
,
len
(
tasks
),
errFileName
)
else
:
errmsg
=
''
print
(
'
Output written to file
"
{}
"
\n
{}Time elapsed: {}s
'
.
format
(
outFileName
,
errmsg
,
int
(
time
()
-
start
)))
print
(
'
Output written to file
"
{}
"
\n
{}Time elapsed: {}s
'
.
format
(
outFileName
,
errmsg
,
int
(
time
()
-
start
)))
# wrapper to run user function
# wrapper to run user function
...
@@ -91,23 +82,28 @@ def worker(args):
...
@@ -91,23 +82,28 @@ def worker(args):
except
:
except
:
if
(
handleErrors
):
if
(
handleErrors
):
return
(
False
,
traceback
.
format_exc
())
return
(
False
,
traceback
.
format_exc
())
else
:
raise
raise
# Testing
# Testing
def
testFunc
(
arg
):
def
testFunc
(
arg
):
ret
=
str
(
arg
)
+
str
(
os
.
getpid
())
sleep
(
randint
(
2
,
5
))
if
(
randint
(
0
,
100
)
%
2
==
0
):
if
(
randint
(
0
,
100
)
%
2
==
0
):
raise
Exception
(
"
COUCOU
"
+
ret
)
raise
Exception
(
"
COUCOU
"
+
ret
)
return
testFunc2
(
arg
)
def
testFunc2
(
arg
):
ret
=
str
(
arg
)
+
str
(
os
.
getpid
())
sleep
(
randint
(
0
,
3
))
return
ret
return
ret
def
test
():
def
test
():
print
(
"
Error file + debug
"
)
print
(
"
Error file + debug
"
)
parallelRunAndWrite
(
testFunc
,
range
(
10
),
"
testplop
"
,
numProc
=
2
,
errFileName
=
"
errors
"
,
debug
=
True
)
parallelRunAndWrite
(
testFunc
,
range
(
10
),
"
testplop
"
,
numProc
=
2
,
errFileName
=
"
errors
"
,
debug
=
True
)
print
(
"
Error file
"
)
print
(
"
Error file
"
)
parallelRunAndWrite
(
testFunc
,
range
(
10
),
"
testplop
"
,
numProc
=
2
,
errFileName
=
"
errors
"
)
parallelRunAndWrite
(
testFunc
,
range
(
10
),
"
testplop
"
,
numProc
=
2
,
errFileName
=
"
errors
"
)
print
(
"
No error file/ no rror
"
)
parallelRunAndWrite
(
testFunc2
,
range
(
10
),
"
testplop
"
,
numProc
=
2
)
print
(
"
No error file
"
)
print
(
"
No error file
"
)
parallelRunAndWrite
(
testFunc
,
range
(
10
),
"
testplop
"
,
numProc
=
2
)
parallelRunAndWrite
(
testFunc
,
range
(
10
),
"
testplop
"
,
numProc
=
2
)
...
...
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