Reputation: 441
I've written a fairly basic decorator:
def test_speed(f, *args, **kwargs):
"""This decorator will print out the time taken for input function to run."""
@wraps(f)
def wrapper():
"""Wrapper function returned by the outer function."""
start = time.time()
to_return = f(*args, **kwargs)
end = time.time()
print(f"The function {__name__} completed in {end-start} seconds.")
return to_return
return wrapper
In a python script called decorators in a project called tools. I've added this project to my configuration in a second project which I'm using to practice using the multiprocessing module. I wrote some test functions to check the speed of multiprocessing some loops:
""" A script to practice using multiprocessing effectively in python."""
from decorators import *
from multiprocessing import *
def _loop(i, process_number):
for i in range(i):
if i % 500 == 0:
print(f'{i} iterations of loop {process_number}.')
def serial_looping():
_loop(10000, 'one')
_loop(10000, 'two')
_loop(10000, 'three')
@test_speed
def parallel_looping():
loop_one = Process(target=_loop, args=(10000, 'one'))
loop_two = Process(target=_loop, args=(10000, 'two'))
loop_three = Process(target=_loop, args=(10000, 'three'))
if __name__ == '__main__':
serial_looping()
parallel_looping()
def serial_looping():
_loop(10000, 'one')
_loop(10000, 'two')
_loop(10000, 'three')
@test_speed
def parallel_looping():
loops = []
loops.append(Process(target=_loop, args=(10000, 'one')))
loops.append(Process(target=_loop, args=(10000, 'two')))
loops.append(Process(target=_loop, args=(10000, 'three')))
for loop in loops:
loop.start()
print('loop')
if __name__ == '__main__':
serial_looping()
parallel_looping()
My issue is that when the wrapped function is called, in place of name it is stating the wrapper's project name, decorator, like so:
The function decorators completed in 5.841255187988281e-05 seconds.
surely this should read:
The function serial_looping completed in 5.841255187988281e-05 seconds.
Upvotes: 1
Views: 79
Reputation: 36682
You should call __name__
on the function f
:
print(f"The function {f.__name__} completed in {end-start} seconds.")
The reason is that __name__
, by default refers to the enclosing function; I don't think @wraps
overrides this behavior; it cannot know what precisely you intend to print.
Upvotes: 1