I want to implement a list of tasks, but I don't know which approach of the two is the best:
First:
class Tasks(object):
def __init__(self) -> None:
self.tasks = []
def add(self, process, instance) -> None:
self.tasks.append((process, instance, Result()))
# example usage:
tasks = Tasks()
tasks.add('process-1', 'instance-1')
or maybe:
class Task(object):
def __init__(self, process, instance) -> None:
self.process = process
self.instance = instance
self.result = Result()
# example usage:
tasks = []
task = Task('process-1', 'instance-1')
tasks.append(task)
The goal is to keep somewhere processes and their results when doing parallel tasks (multiprocessing library). Maybe some way is more pythonic? or just more OOP.
CodePudding user response:
Highly opinionated, and probably more suited for Software Engineering SE, but each has its own advantages and disadvantages.
The first one is supposedly easier to use and a more "OOP" fashion, but I dislike it for various reasons: It restricts your containers to that specific task list, it's harder to extend, and you lose a lot of built-in list operations when using that structure (if you don't inherit from collections.abc.MutableSequence
).
The second one is more akin to anemic object model. Some consider this to be an anti-pattern but I find numerous advantages in this approach here:
- You have the builtin list capabilities.
- Your task object is simple and can be extended / subclassed.
- You can easily switch the container (set, list, tuple...).
- Easier to handle multi-threading and multi-processing when the container object does not need to guard against such concurrent operations.