Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
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
FSL
fslpy
Commits
44748470
Commit
44748470
authored
8 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
async.run accepts a callback function to be called if the task raises an
error.
parent
52316ae7
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/utils/async.py
+35
-22
35 additions, 22 deletions
fsl/utils/async.py
with
35 additions
and
22 deletions
fsl/utils/async.py
+
35
−
22
View file @
44748470
...
...
@@ -56,13 +56,17 @@ def _haveWX():
return
False
def
run
(
task
,
onFinish
=
None
,
name
=
None
):
def
run
(
task
,
onFinish
=
None
,
onError
=
None
,
name
=
None
):
"""
Run the given ``task`` in a separate thread.
:arg task: The function to run. Must accept no arguments.
:arg onFinish: An optional function to schedule on the ``wx.MainLoop``
once the ``task`` has finished.
:arg onFinish: An optional function to schedule (on the ``wx.MainLoop``,
via :func:`idle`) once the ``task`` has finished.
:arg onError: An optional function to be called (on the ``wx.MainLoop``,
via :func:`idle`) if the ``task`` raises an error. Passed
the ``Exception`` that was raised.
:arg name: An optional name to use for this task in log statements.
...
...
@@ -78,33 +82,42 @@ def run(task, onFinish=None, name=None):
haveWX
=
_haveWX
()
def
wrapper
():
log
.
debug
(
'
Running task
"
{}
"
...
'
.
format
(
name
))
task
()
log
.
debug
(
'
Task
"
{}
"
finished
'
.
format
(
name
))
if
(
onFinish
is
not
None
):
import
wx
# Calls the onFinish or onError handler
def
callback
(
cb
,
*
args
,
**
kwargs
):
if
cb
is
None
:
return
if
haveWX
:
idle
(
cb
,
*
args
,
**
kwargs
)
else
:
cb
(
*
args
,
**
kwargs
)
log
.
debug
(
'
Scheduling task
"
{}
"
finish handler
'
'
on wx.MainLoop
'
.
format
(
name
))
# Runs the task, and calls
# callback functions as needed.
def
wrapper
():
# Should I use the idle function here?
wx
.
CallAfter
(
onFinish
)
try
:
task
()
log
.
debug
(
'
Task
"
{}
"
finished
'
.
format
(
name
))
callback
(
onFinish
)
except
Exception
as
e
:
log
.
warn
(
'
Task
"
{}
"
crashed
'
,
exc_info
=
True
)
callback
(
onError
,
e
)
# If WX, run on a thread
if
haveWX
:
log
.
debug
(
'
Running task
"
{}
"
on thread
'
.
format
(
name
))
thread
=
threading
.
Thread
(
target
=
wrapper
)
thread
.
start
()
return
thread
# Otherwise run directly
else
:
log
.
debug
(
'
Running task
"
{}
"
directly
'
.
format
(
name
))
task
()
log
.
debug
(
'
Running task
"
{}
"
finish handler
'
.
format
(
name
))
onFinish
()
log
.
debug
(
'
Running task
"
{}
"
directly
'
.
format
(
name
))
wrapper
()
return
None
...
...
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