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.
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.