Intercepting method calls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Leslie

    Intercepting method calls

    I want to write a package to simulate a distributed system with objects
    at various locations interacting with each other. What I'd like to do is
    simulate network delays between distant objects using an event queue.

    That is, when I call A.B(foo), I want this call to be intercepted
    somehow, and the call to B added to a central event queue thread, and is
    called later, possibly after other previously enqued method calls. The
    central queue would also do some accounting to keep track of things like
    messages sent, message size, etc.

    The idea is that you just write these objects as if they were local
    objects communicating normally, and all the network simulation is done
    for you transparently - perhaps by some 'NetworkObject' superclass.

    I took a look at the language reference and it is not immediately clear
    to me how or if I could do this in Python, perhaps I need to override
    the __call__ methods for all the instancemethods in the class? I'm not
    even sure how I could do this... Any help or suggestions appreciated.

    Thanks,
    Matt
  • Diez B. Roggisch

    #2
    Re: Intercepting method calls

    Matt Leslie wrote:
    [color=blue]
    > I took a look at the language reference and it is not immediately clear
    > to me how or if I could do this in Python, perhaps I need to override
    > the __call__ methods for all the instancemethods in the class? I'm not
    > even sure how I could do this... Any help or suggestions appreciated.[/color]

    A way to accomplish this is the usage of metaclasses. A metaclass gives you
    a sort of callback that gets invoked with the classname, the base classes
    and the class dictionary before the actual class object is created. Here
    you then can creat wrappers around all callables that implement your
    desired behaviour.


    See this reciepe:




    --
    Regards,

    Diez B. Roggisch

    Comment

    • Matt Leslie

      #3
      Re: Intercepting method calls

      Diez B. Roggisch wrote:
      [color=blue]
      > A way to accomplish this is the usage of metaclasses. A metaclass gives you
      > a sort of callback that gets invoked with the classname, the base classes
      > and the class dictionary before the actual class object is created. Here
      > you then can creat wrappers around all callables that implement your
      > desired behaviour.
      >[/color]

      Thanks, I'll take a look at that.

      Ta,
      Matt

      Comment

      Working...