2

Python class overwrite data

I am creating Task Manager in python using threads but when i instance two task the method of second task overwrite the method from frist task. I don't want this happen

Class Task

import time
import sys
from threading import Thread


class Task(Thread):
    name = ""

    timeout = 300

    data = {}

    debug = False

    """
        Config.type -> repeat, once, forever

        repeat : if return True on call
        once : repeat only one time
        forever : repeat for time undefined
    """

    task_config = {
        "type": "repeat",
        "time": 5,
        "call": lambda: False,
        "limit_call": False,
        "getDataFrom": ()
    }

    def __init__(self, name, **options):

        Thread.__init__(self)

        self.name = name
        self.task_config.update(options)

    def set_debug(self, active):
        self.debug = active

    def set_config(self, options):
        print "================================================"
        print self.name
        print "Before Changes"
        print options
        print self.task_config

        self.task_config.update(options)
        print "After Changes"
        print self.task_config



    def run(self):

        method = self.task_config['call']
        sleep_time = self.task_config['time']

        print method

        if not hasattr(method, '__call__'):
            raise TypeError('<TaskManager> config.call isn\'t a function')

        response = True

        while response:

            if self.debug:
                sys.stdout.write("Call: " + self.name)

            response = method.__call__(*self.data)

            sys.stdout.flush()
            time.sleep(sleep_time)


def test():
    print 324342
    return False


def test2():
    print "Test 2 called\n"
    return False


manager = TaskManager()

task = manager.create_task("Trhead 1\n", call=test, time=2)
task2 = manager.create_task("Trhead 2\n", call=test2, time=1)

task.start()
task2.start()

Output

================================================
Name: Trhead 1

Before Changes
Option: {'call': <function test at 0x0000000001F509E8>, 'time': 2}
Class Config: {'call': <function <lambda> at 0x0000000001F50C18>, 'getDataFrom': (), 'type': 'repeat', 'limit_call': False, 'time': 5}
After Changes
Class Config: {'call': <function test at 0x0000000001F509E8>, 'limit_call': False, 'time': 2, 'type': 'repeat', 'getDataFrom': ()}
================================================
Name: Trhead 2

Before Changes
Option: {'call': <function test2 at 0x0000000001F50E48>, 'time': 1}
Class Config: {'call': <function test at 0x0000000001F509E8>, 'limit_call': False, 'time': 2, 'type': 'repeat', 'getDataFrom': ()}
After Changes
Class Config: {'call': <function test2 at 0x0000000001F50E48>, 'limit_call': False, 'time': 1, 'type': 'repeat', 'getDataFrom': ()}
<function test2 at 0x0000000001F50E48>
Test 2 called
Guilherme Soares
  • 726
  • 3
  • 7
  • 19

1 Answers1

3

Every variable declared inside the class definition, but not inside a method are class variables. So if you want an "object variable(an instance variable)" you must declare it inside the method:

    class MyClass:
       def something(self):
          self.variable_for_the_object = 1

This answer has more details about it: https://stackoverflow.com/a/69067/1399290

Community
  • 1
  • 1