Module implementing a multi-threaded hdd io queue.
Project description
mhddq
Module implementing a multi-threaded hdd io queue.
pip install mhddq
import mhddq
import time
# calls are enqueued instead of being called immediately
# 2 alternating io threads handle all io functions
# ideally while one thread is dealing with queue maintenance the
# other thread is using the hdd, and then they trade off when done
# after a function has been completed by the io threads its output
# is enqueued for a third thread to handle the callback as to not
# interfere with the two io threads
mhddq.file_exists(callback = cb_file_exists, filename = './file.txt')
def cb_file_exists(args):
# args = {
# 'action': str, # function name, 'file_exists' in this case
# 'result': bool, # if the function encoutered any errors then this is False
# 'params': dict, # the params you initially passed, { 'callback': function, 'filename': str }
# 'output': dict # the output of the function, in this case { 'exists': bool }
# # if result is False then this would be { 'exception': str }
# }
# print the result, 'file.txt exists: False'
print(args['params']['filename'] + ' exists: ' + str(args['output']['exists']))
# if a callback throws an exception it is caught and printed to stderr
# if multiple operations need to be grouped together you can use an oplist
fileops = mhddq.create_empty_oplist()
# oplists have their own sets of function calls
# in practice it ends up being the same as mhddq.file_exists()
# except the io thread considers the entire oplist as a single
# operation and won't yield to it's sister thread until the list
# has been completed
mhddq.oplist_file_exists(oplist = fileops, callback = cb_file_exists, filename = './file.txt')
# you can add an oplist directly to the io queue like this
mhddq.enqueue_oplist(fileops)
# or you can embed the oplist in another oplist
superlist = mhddq.create_empty_oplist()
mhddq.embed_oplist(superlist, fileops)
# embedded lists are processed as a single operation inside it's
# parent list, while it's parent list is processed as a single operation
# in the io queue
mhddq.enqueue_oplist(superlist)
# when it is time to turn off the app you can do a graceful shutdown
# this will prevent any new items from being enqueued and blocks the
# calling thread until the io and callback threads have cleared their
# queues and exited their processing loops
mhddq.shutdown()
# other threads can wait for a graceful shutdown by using a loop like this
while mhddq.is_shutdown() is False:
time.sleep(0.01)
# once shutdown, mhddq can not be restarted without restarting the app
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
mhddq-0.3.0.tar.gz
(14.4 kB
view details)
Built Distribution
mhddq-0.3.0-py2.py3-none-any.whl
(13.8 kB
view details)
File details
Details for the file mhddq-0.3.0.tar.gz
.
File metadata
- Download URL: mhddq-0.3.0.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | db2431576bd87fd34f2a47aea6dda26d0d4ab11698830892411e5812c76990ff |
|
MD5 | e3bab79bf7689e0fc11a57f44d8cd918 |
|
BLAKE2b-256 | 4eccb079dc6e42a4dfbe8d4f8a1f2bd8f43bee021e2e01e330291d3e0369dd88 |
File details
Details for the file mhddq-0.3.0-py2.py3-none-any.whl
.
File metadata
- Download URL: mhddq-0.3.0-py2.py3-none-any.whl
- Upload date:
- Size: 13.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0cc9e526ce98934cc16e14201db68733963c687f324a232acfa1cbab402efa5 |
|
MD5 | 4eba36a3cf22e2fcb3adc0a4d2a1ead9 |
|
BLAKE2b-256 | 86a165878d5f540b9445ff4a00406ad77eb17f2f47a86b8d5bba771b0fd7c2a5 |