kquick / Thespian

Python Actor concurrency library
MIT License
189 stars 24 forks source link

initialize actor with internal state variables #56

Closed davideps closed 4 years ago

davideps commented 4 years ago

I found this issue from a few years ago: https://github.com/thespianpy/Thespian/issues/18 but I don't think it fits my case. How would I initialize a simple actor with its own id number and perhaps other state variables like:

class Agent(Actor):
    def __init__(self, myid):
        self.myid = myid
        super().__init__()
        print("Actor", self.myid, "started.")

    def receiveMessage(self, message, sender):
        if message["command"] == "greet":
            print("Agent received greet command from", sender, ".")
            print("Hello", message["name"])
kquick commented 4 years ago

What you have is fine (although I recommend standard python convention of adding *args, **kw to the __init__ signature and passing those to the base class initializer as in super().__init__(*args, **kw).

The Actor __init__() cannot perform any actor-related operations (e.g. calling self.send()) and as with the issue you referenced, it should defer initialization requiring external interactions or information to be handled by a received message instead.

davideps commented 4 years ago

Thank you for the quick reply. I'll send values for initialization via a message, probably a dictionary.

On Thu, Feb 13, 2020, 5:37 PM Kevin Quick notifications@github.com wrote:

What you have is fine (although I recommend standard python convention of adding *args, *kw to the init signature and passing those to the base class initializer as in super().init(args, **kw).

The Actor init() cannot perform any actor-related operations (e.g. calling self.send()) and as with the issue you referenced, it should defer initialization requiring external interactions or information to be handled by a received message instead.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/kquick/Thespian/issues/56?email_source=notifications&email_token=AANTN7LVYXFQ5GDCEZTSODDRCVSKFA5CNFSM4KUUIV4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELVOMTA#issuecomment-585819724, or unsubscribe https://github.com/notifications/unsubscribe-auth/AANTN7LVQZJ3ZJAZNK62DDLRCVSKFANCNFSM4KUUIV4A .

kquick commented 4 years ago

You're welcome, and please feel free to re-open this issue if you have further difficulties or questions.

tylerhjones commented 3 years ago

Might be my python knowledge, but I get an error with the following

class DateGroupActor(ActorTypeDispatcher):
  def __init__(self, *args, **kw):
    super().__init__(*args, **kw)
    self.seat_percent = self.createActor(SeatPercentage)

AttributeError: 'DateGroupActor' object has no attribute '_myRef'

I have also tried putting the self. attribute assignment before calling super, no difference. as far as I can tell, Ive followed the above suggestion, any idea what is wrong?