从嵌套装饰器获取函数名称

时间:2018-05-15 12:35:59

标签: python python-2.7 decorator python-decorators

我正在尝试使用2个装饰器时获得正确的函数名称。

1>个人资料 - 2018-05-15 18:41:11.494 WARN 2709 --- [cTaskExecutor-1] s.a.r.l.ConditionalRejectingErrorHandler : Execution of Rabbit message listener failed. org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener method 'public void com.amqp.handleException.demo.DemoApplication.handle(com.amqp.handleException.demo.DemoApplication$Dispatch)' threw exception at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:140) ~[spring-rabbit-1.7.7.RELEASE.jar:na] at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:106) ~[spring-rabbit-1.7.7.RELEASE.jar:na] at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:856) ~[spring-rabbit-1.7.7.RELEASE.jar:na] at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:779) ~[spring-rabbit-1.7.7.RELEASE.jar:na] at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:105) [spring-rabbit-1.7.7.RELEASE.jar:na] at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:208) ~[spring-rabbit-1.7.7.RELEASE.jar:na] at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1349) [spring-rabbit-1.7.7.RELEASE.jar:na] at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:760) ~[spring-rabbit-1.7.7.RELEASE.jar:na] at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1292) [spring-rabbit-1.7.7.RELEASE.jar:na] at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:1262) [spring-rabbit-1.7.7.RELEASE.jar:na] at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1800(SimpleMessageListenerContainer.java:105) [spring-rabbit-1.7.7.RELEASE.jar:na] at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1518) [spring-rabbit-1.7.7.RELEASE.jar:na] at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111] Caused by: com.amqp.handleException.demo.DemoApplication$UndispatchException: Dispatch still looking for a driver at com.amqp.handleException.demo.DemoApplication.handle(DemoApplication.java:47) ~[classes/:na] at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111] at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:180) ~[spring-messaging-4.3.15.RELEASE.jar:4.3.15.RELEASE] at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:112) ~[spring-messaging-4.3.15.RELEASE.jar:4.3.15.RELEASE] at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:49) ~[spring-rabbit-1.7.7.RELEASE.jar:na] at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:126) ~[spring-rabbit-1.7.7.RELEASE.jar:na] ... 12 common frames omitted

2 - ;自定义时序装饰器

from memory_profiler import profile

按以下定义使用它们

def timing(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        start = time()
        result = f(*args, **kwargs)
        end = time()
        print 'Elapsed time: {} - {}'.format(wrapper.__name__, end - start)
        return result

    return wrapper 

问题是这两个函数单独运行良好但是当一起使用时,我没有通过时序装饰器获得正确的名称。我总是得到@timing @profile def my_function(): something..... 而不是实际的函数名。

如何获取实际的函数名而不是将“wrapper”作为函数名?

0 个答案:

没有答案