1

How can I call thread on a class method passing it 'self'? I have a class defined as follows and want to call class method in new thread with self as argument. I tried following but self is not passed as argument

cust_obj = Customer()
thread.start_new_thread(cust_obj.process, ())  

class Customer():
    def __init__(self):
        pass
    def process(self):
        self.fetch_data()
        self.serialize_data()
    def fetch_data(self):
        # Fetch data logic
        pass
    def serialize_data(self):
        # Serialize fetched data
        pass
4
  • Ought to work (keyword: bound method). What's the error message? Commented Dec 29, 2012 at 13:53
  • That ought to work. Anyway, a simple workaround could be: thread.start_new_thread((lambda: Customer().process()), ()) Commented Dec 29, 2012 at 14:04
  • Can't give any assistance until you state what fails. Commented Dec 29, 2012 at 14:40
  • 1
    unrelated to your current issue but you should use threading module instead of obsolete thread. Commented Dec 29, 2012 at 15:01

1 Answer 1

2

I believe you should put the class definition before the instance creating. Then it will work.

class Customer():
    ...
cust_obj = Customer()
thread.start_new_thread(cust_obj.process, ())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.