Threading Decorator [Python]

浪尽此生 提交于 2020-12-13 03:37:55

问题


i'm trying to create a simple program using python socket and threading library. I would like to automatize the following procedure using a decorator:

t = threading.Thread(target=function, args=(arg1, arg2))
t.start()

the program is structured using OOP so I defined a subclass inside the main one to contain all the decorators (I've read about this method in this article: https://medium.com/@vadimpushtaev/decorator-inside-python-class-1e74d23107f6). Therefore I have a situation like this:

class Server(object):

    class Decorators(object):

        @classmethod
        def threaded_decorator(cls, function):
            def inner_function():
                function_thread = threading.Thread(target=function)
                function_thread.start()
            return inner_function

    def __init__(self, other_arguments):
        # other code
        pass

    @Decorators.threaded_decorator
    def function_to_be_threaded(self):
        # other code
        pass

But when I try to run, I get the following error: TypeError: function_to_be_threaded() missing one required argument: 'self'. I suspect that the issue is located in the part when I call the threading.Thread(target=function) which somehow doesn't pass the entire function self.function_to_be_threaded. Hence, if you know how to fix this, please can you tell me?. Also, can you tell me if there is a way to implement a decorator that accepts argument which will pass to the Thread class as args=(arguments_of_the_decorator)?

Thank very much you for your time and excuse my English, I'm still practising it


回答1:


Use *args syntax to move the arguments.In other words, use *args to collect all the positional arguments as a tuple and move it threading.Thread as args.

import threading
import time
class Server(object):

    class Decorators(object):

        @classmethod
        def threaded_decorator(cls, function):
            def inner_function(*args):
                function_thread = threading.Thread(target=function,args=args)
                function_thread.start()
            return inner_function

    def __init__(self, count,sleep):
        self.count = count
        self.sleep = sleep

    @Decorators.threaded_decorator
    def function_to_be_threaded(self,id):
        for xx in range(self.count):
            time.sleep(self.sleep)
            print("{} ==> {}".format(id,xx))
           

>>> Server(6,1).function_to_be_threaded('a')
>>> Server(2,3).function_to_be_threaded('b')

a ==> 0
a ==> 1
a ==> 2
b ==> 0
a ==> 3
a ==> 4
a ==> 5
b ==> 1

see also How can I pass arguments from one function to another?



来源:https://stackoverflow.com/questions/64555496/threading-decorator-python

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