-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for multi-threadded Queue loading #45
Merged
campb303
merged 7 commits into
staging
from
feature-implement-multiparsing-when-loading-multiple-queues-and-items
Aug 2, 2021
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
90ba300
import multiprocessing package in queue.py
benne238 d8e722b
Added logic to parse multiple items at once in a given queue using th…
benne238 5a30f2d
Added code to wait until all of the items are loaded before returning…
benne238 55cb9ad
Modified load_queues function in queue.py to accept *args (strings th…
benne238 81e28e5
created a custom class that allows subprocesses to spawn other subpro…
benne238 ab565ec
added logic to parse multiple queues with multiparsing using the the …
benne238 49ad6e8
Docs cleanup
campb303 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import multiprocessing | ||
import multiprocessing.pool | ||
import os, re | ||
from pathlib import Path | ||
from .item import Item | ||
|
@@ -73,14 +75,20 @@ def __get_items(self, headers_only: bool) -> list: | |
list: a list of items for this Queue | ||
""" | ||
items = [] | ||
valid_items = [] | ||
multi_item_processes = multiprocessing.Pool(processes=multiprocessing.cpu_count()) | ||
|
||
for item in os.listdir(self.path): | ||
item_path = Path(self.path, item) | ||
|
||
is_file = True if os.path.isfile(item_path) else False | ||
|
||
if is_file and is_valid_item_name(item): | ||
items.append(Item(self.name, item, headers_only)) | ||
valid_items.append(item) | ||
|
||
items = multi_item_processes.starmap_async(Item, [(self.name, item, headers_only) for item in valid_items]).get() | ||
multi_item_processes.close() | ||
multi_item_processes.join() | ||
|
||
return items | ||
|
||
|
@@ -178,15 +186,40 @@ def get_queue_counts() -> list: | |
|
||
return sorted_queue_info | ||
|
||
def load_queues() -> list: | ||
"""Return a list of Queues for each queue. | ||
def load_queues(*queues, headers_only: bool = True) -> list: | ||
"""Returns a list of queues | ||
|
||
Example: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add example. |
||
[example] | ||
|
||
Args: | ||
headers_only (bool, optional): Weather or not the content of items in the queue should be loaded. Defaults to True. | ||
*queues: List of strings that represent Queue names. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add type to queues. |
||
|
||
Returns: | ||
list: list of Queues for each queue. | ||
list: A list of all the queues that were given as arguments, or all of the queues if no queues were specified | ||
""" | ||
queues = [] | ||
|
||
for queue in get_valid_queues(): | ||
queues.append(Queue(queue)) | ||
# custom class creation based on stackoverflow answer | ||
class NoDaemonProcess(multiprocessing.Process): | ||
# make 'daemon' attribute always return False | ||
def _get_daemon(self): | ||
return False | ||
def _set_daemon(self, value): | ||
pass | ||
daemon = property(_get_daemon, _set_daemon) | ||
|
||
# We sub-class multiprocessing.pool.Pool instead of multiprocessing.Pool | ||
# because the latter is only a wrapper function, not a proper class. | ||
class MyPool(multiprocessing.pool.Pool): | ||
Process = NoDaemonProcess | ||
|
||
if len(queues) == 0: queues_to_load = get_valid_queues() | ||
elif len(queues) == 1: return [Queue(name=queues[0], headers_only=headers_only)] | ||
else: queues_to_load = queues | ||
|
||
multi_queue_process = MyPool(processes=multiprocessing.cpu_count()) | ||
loaded_queues = multi_queue_process.starmap_async(Queue, [(queue, headers_only) for queue in queues_to_load]).get() | ||
multi_queue_process.close() | ||
multi_queue_process.join() | ||
|
||
return queues | ||
return loaded_queues |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add type to
queues