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?

Martin Dusek
  • 1,170
  • 3
  • 16
  • 41

1 Answers1

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'
vadimhmyrov
  • 169
  • 7