0

I have Python class with indefinitely blocking task method

class A(object):
   def __init__(self):
       # start task

   def task(self):
       while True:
           #do some work

I want to start execution of the task in constructor of A. It will probably need to be run in its own thread as the task is blocking. How to do that in Python 2.7?

2

1 Answer 1

0

Like mentioned in comments, there's a module threading that seems to exactly fit your task. Example:

import threading

class A(object):
   def __init__(self):
       threading.Thread(target=self.task).start()

   def task(self):
       print 'Hello from a Thread'

a = A()

# output: 'Hello from a Thread'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.