k3xji a écrit :
(snip)
My problem is that I have a very-multithreaded application that I want
to profile dynamically. And I want to see how many functions are
called/how much time spent on them.(I tried Profilers but lack of
multithreading support I cannot do what I want to do.)
???
I didn't found any mention of such a limitation on the profile /
cProfile doc:
http://www.python.org/doc/2.6/library/profile.html#module-cProfile
So, from your answers above, if I use a decorator I will not be able
to understand that the function has finished processing.
Yes you will:
def log(func):
def _logged(*args, **kw):
print "func", func.__name__, " called with ", args, kw
result = func(*args, **kw)
print "func", func.__name__, " returned ", result
return result
_logged.__name__ = "logged_%s" % func.__name__
_logged.__doc__ = func.__doc__
return _logged
> I think problem is clarified now. Maybe you have other suggestions?
Yes.
1/ the
@decorator
def func():
pass
syntax is really syntactic sugar for
def func():
pass
func = decorator(func)
2/ nothing prevents you from dynamically rebinding methods at runtime:
def log(func):
# cf below
if getattr(func, "_decorated", False):
# already decorated, just return the func or method as is
return func
def _logged(*args, **kw):
print "func", func.__name__, " called with ", args, kw
result = func(*args, **kw)
print "func", func.__name__, " returned ", result
return result
_logged.__name__ = "logged_%s" % func.__name__
_logged.__doc__ = func.__doc__
# IMPORTANT : mark the function / method as already decorated
_logged._decorated = True
return _logged
class Foo(object):
def bar(self):
pass
Foo.bar = log(Foo.bar)
3/ Now fact is that even all this won't probably be enough to implement
a robust generic profiler - something which obviously requires a deep
understanding of the language *and* it's implementation.
--
http://mail.python.org/mailman/listinfo/python-list