Class-based actor #9
Comments
It seems like the |
Hi! Thank you for the kind words. Out of curiosity, can you show me a minimal example of how you were thinking this would work? |
As i realized what i described above is not the case, here's what i am trying to do:
Any class-based task that i have basically will extend What i realized is that, declaring tasks such that:
Won't cut it because when the worker starts, the task has to be instantiated to be recognized by the broker via
At the very end of the file but that doesn't look convenient. Other ideas could be to call Any other ideas about how this could be done other than the above? |
How about something like this? import dramatiq
class actor(type):
def __new__(cls, name, bases, attrs):
clazz = super().__new__(cls, name, bases, attrs)
abstract = attrs.get("abstract", False)
if not abstract:
return dramatiq.actor(clazz())
return clazz
class Actor(metaclass=actor):
abstract = True
@property
def __name__(self):
return type(self).__name__
def __call__(self, *args, **kwargs):
return self.perform(*args, **kwargs)
def perform(self):
raise NotImplementedError
class CoreTask(Actor):
abstract = True
def get_task_name(self):
raise NotImplementedError
def perform(self):
print(f"Performing task {self.get_task_name()}")
class FooTask(CoreTask):
def get_task_name(self):
return "Foo"
class BarTask(CoreTask):
def get_task_name(self):
return "Bar"
if __name__ == "__main__":
FooTask.send()
BarTask.send() |
Yea this looks pretty good actually. Would this be something we can have in dramatiq core? |
Yup, I think this is a useful addition so I'll add it for |
This'll be a part of |
Excellent, looking forward to 0.13 |
Released! |
Hi Bogdanp, how can I pass variables to my class? Meaning when I call the FooTask.send() as stated in your example, I want to do the following: FooTask.send(param_1='test', param_2=20). Is this possible with the GenericActor? |
Hi @MaxRichter, when you subclass class Adder(GenericActor):
def perform(self, a, b):
print(a + b)
Adder.send(1, 2) |
Hi @Bogdanp, thank you for the quick answer. This is working for me although I am getting the following warning: Signature of method 'Adder.perform()' does not match signature of base method in class 'GenericActor'. I executed the case you provided - maybe I understood the concept of subclasses wrong or can I ignore this warning? |
Where are you getting that from? mypy? If so, you can ignore it. |
I am getting this from PyCharm IDE. By the way, is there an option to do "store_results=True" in GenericActor similar as in the function definition |
Yes, you can provide options like that via the inner |
Great, works! Thank you a lot for the hints. |
Hello,
I've been looking for a celery alternative for quite a while, thanks for taking the time to make this.
A suggestion i'd like to make is to add the ability of defining class-based actor where grouping common functionality among different "tasks" or "actors" is possible.
Thank you.
The text was updated successfully, but these errors were encountered: