Accessing singleton object

29 views
Skip to first unread message

Erdem Eser Ekinci

unread,
Jul 22, 2021, 9:29:48 AM7/22/21
to thespian.py
Hi, 

I defined a singleton class that works like a cache solution to share values between actors (Please do not suggest to message them, because almost all actors uses these values).

Problem is that instance differentiates between actors even they are in same actor system.

How can make them using same object? 

Below you can find my simple singleton implementation.  

Thanks for your support, 


class FastCache(object):
   __instance = None

   @staticmethod
   def instance():
      """ Static access method. """
      if FastCache.__instance is None:
      FastCache()
         return FastCache.__instance

   def __init__(self):
      """ Virtually private constructor. """
      if FastCache.__instance is not None:
         raise Exception("This class is a singleton!")
      else:
         FastCache.__instance = self
         self.map = {}

   def cache(self, uri, record):
      self.map[uri] = record
      return

Kevin Quick

unread,
Jul 26, 2021, 10:59:10 AM7/26/21
to Erdem Eser Ekinci, thespian.py
Hi Erdem,

I'm afraid you aren't going to be able to share memory between actors
reliably.

Fundamentally the Actor model itself does not support shared resources
and all data must be exchanged via messages.

Specific to Thespian, multi-process based systems (e.g.
multiprocTCPBase) run each actor as a separate system process, so the
process boundaries will prevent sharing memory in this manner. It
would work for the simpleSystemBase, which runs in a single process
(sacrificing any concurrency), but that is a violation of the actor
boundaries and would also prevent changing the system base as well.

Regards,
Kevin
> --
> You received this message because you are subscribed to the Google Groups "thespian.py" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to thespianpy+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/thespianpy/33900fdb-96d3-4b29-a89a-722eef9829e7n%40googlegroups.com.



--
-KQ

Erdem Eser Ekinci

unread,
Jul 27, 2021, 4:10:25 AM7/27/21
to thespian.py
Thanks Kevin, 

I know what I want does not seams suitable for actor-based system. 

Best. 

Zvonimir Križ

unread,
Oct 19, 2021, 4:46:24 AM10/19/21
to thespian.py
Hello,

I'm sorry for a late reply, but I just came across the same thing recently, so I'd like to share my thoughts and possibly relive the discussion:
Maybe is too obvious, but if the values from singleton object does not change too often, you can share it by a simple config file. My case is that is changed rarely and in this case the "changer actor" notifies registered actors to reload the change.
Another option would be to use something like Redis or Memcached.

Regards,
Zvone

Erdem Eser Ekinci

unread,
Dec 13, 2021, 8:33:25 AM12/13/21
to thespian.py
Thanks, I preferred Redis. However, somehow there is a need to read file and share its content without doing IO for every use. 

Best.  

Reply all
Reply to author
Forward
0 new messages