Threads with decorators

蓝咒 提交于 2019-12-10 04:08:23

问题


I'm trying to implement threading(with using decorators) to my application, but can't understand some things about locks and managing threads.

import threading

def run_in_thread(fn):
    def run(*k, **kw):
        t = threading.Thread(target=fn, args=k, kwargs=kw)
        t.start()
    return run

class A:
    @run_in_thread
    def method1(self):
        for x in range(10000):
            print x


    @run_in_thread
    def method2(self):
        for y in list('wlkefjwfejwiefwhfwfkjshkjadgfjhkewgfjwjefjwe'):
            print y

    def stop_thread(self):
        pass

c = A()
c.method1()
c.method2()
  1. As I understand, method1 and method2 are not synchronized, but synchronizing of that stuff implementing with help of locks. How I can add locks to my decorator-function?

  2. How can I realize method for stopping long threads using decorators?


回答1:


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.




回答2:


  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.




回答3:


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)


来源:https://stackoverflow.com/questions/14234547/threads-with-decorators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!