Threads with decorators

  1. If you want to synchronize the two threads you simply need to add locks inside the decorated functions, not the decorators themselves.

  2. There is not a simple way to directly stop a Thread, only way is to use an Event to signal the thread it must exit.

For threading decorators you can take a look at pebble.


Maybe Semaphores could help in decorators, something like this - calculating factorial numbers from 1 to 1000:

import threading

from functools import wraps
from math import factorial


DIC = {}

def limit(number):
    ''' This decorator limits the number of simultaneous Threads
    '''
    sem = threading.Semaphore(number)
    def wrapper(func):
        @wraps(func)
        def wrapped(*args):
            with sem:
                return func(*args)
        return wrapped
    return wrapper

def async(f):
    ''' This decorator executes a function in a Thread'''
    @wraps(f)
    def wrapper(*args, **kwargs):
        thr = threading.Thread(target=f, args=args, kwargs=kwargs)
        thr.start()
    return wrapper

@limit(10)     # Always use @limit as the outter decorator
@async
def calcula_fatorial(number):
    DIC.update({number: factorial(number)})

@limit(10)
def main(lista):
    for elem in lista:
        calcula_fatorial(elem)


if __name__ == '__main__':
    from pprint import pprint
    main(range(1000))
    pprint(DIC)

If you extend the function to

def run_in_thread(fn):
    def run(*k, **kw):
        t = threading.Thread(target=fn, args=k, kwargs=kw)
        t.start()
        return t # <-- this is new!
    return run

i. e., let the wrapper function return the created thread, you can do

c = A()
t1 = c.method1()
t1.join() # wait for it to finish
t2 = c.method2()
# ...

i. e, get the thread where the original method runs in, do whatever you want with it (e. g. join it) and only then call the next method.

If you don't need it in a given case, you are free to omit it.