Intercepting method calls

M

Matt Leslie

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
 
D

Diez B. Roggisch

Matt said:
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.

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:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/198078
 
M

Matt Leslie

Diez said:
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.

Thanks, I'll take a look at that.

Ta,
Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top