Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
qutil
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
qutech
qutil
Commits
77405c72
Verified
Commit
77405c72
authored
1 month ago
by
Tobias Hangleiter
Browse files
Options
Downloads
Patches
Plain Diff
Make jobs dict thread-safe
parent
1180e8c4
No related branches found
No related tags found
1 merge request
!177
IO: async datasaver
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
qutil/io.py
+20
-15
20 additions, 15 deletions
qutil/io.py
with
20 additions
and
15 deletions
qutil/io.py
+
20
−
15
View file @
77405c72
...
...
@@ -14,7 +14,7 @@ import textwrap
import
warnings
from
concurrent.futures
import
Future
from
contextlib
import
contextmanager
from
threading
import
Thread
from
threading
import
Thread
,
Lock
from
types
import
ModuleType
from
typing
import
Union
,
Literal
from
unittest
import
mock
...
...
@@ -424,6 +424,7 @@ class AsyncDatasaver:
self
.
_savefn
=
np
.
savez_compressed
if
compress
else
np
.
savez
self
.
jobs
:
dict
[
Future
,
PathType
]
=
{}
self
.
loop
=
asyncio
.
new_event_loop
()
self
.
lock
=
Lock
()
self
.
thread
=
Thread
(
target
=
self
.
_start_loop
,
daemon
=
True
)
self
.
thread
.
start
()
# Make sure all io tasks are done when shutting down the interpreter
...
...
@@ -443,12 +444,13 @@ class AsyncDatasaver:
self
.
loop
.
run_forever
()
def
_mark_done
(
self
,
future
:
Future
):
try
:
future
.
result
()
except
Exception
as
e
:
print
(
f
"
Error in async task for file
{
self
.
jobs
.
get
(
future
)
}
:
{
e
}
"
)
finally
:
self
.
jobs
.
pop
(
future
,
None
)
with
self
.
lock
:
try
:
future
.
result
()
except
Exception
as
e
:
print
(
f
"
Error in async task for file
{
self
.
jobs
.
get
(
future
)
}
:
{
e
}
"
)
finally
:
self
.
jobs
.
pop
(
future
,
None
)
async
def
_save_data
(
self
,
file
:
PathType
,
allow_pickle
:
bool
=
True
,
**
data
):
"""
Coroutine to asynchronously save data in a thread.
...
...
@@ -477,18 +479,21 @@ class AsyncDatasaver:
See :func:`numpy:numpy.savez` for more information.
"""
self
.
jobs
[
# parens necessary for py39..
(
future
:
=
asyncio
.
run_coroutine_threadsafe
(
self
.
_save_data
(
file
,
allow_pickle
=
allow_pickle
,
**
data
),
self
.
loop
))
]
=
file
with
self
.
lock
:
self
.
jobs
[
# parens necessary for py39..
(
future
:
=
asyncio
.
run_coroutine_threadsafe
(
self
.
_save_data
(
file
,
allow_pickle
=
allow_pickle
,
**
data
),
self
.
loop
))
]
=
file
future
.
add_done_callback
(
self
.
_mark_done
)
def
shutdown
(
self
,
timeout
:
float
|
None
=
None
):
"""
Shut down the object, waiting for all I/O tasks to finish.
"""
for
future
,
file
in
self
.
jobs
.
items
():
with
self
.
lock
:
jobs_snapshot
=
list
(
self
.
jobs
.
items
())
for
future
,
file
in
jobs_snapshot
:
try
:
future
.
result
(
timeout
)
except
Exception
as
e
:
...
...
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