import multiprocessing
import time
def sleeper(sec):
time.sleep(sec)
if __name__ == "__main__":
sec=1
process = multiprocessing.Process(target=sleeper,args=[sec])
process.start()
The above code works well. However if the sleeper function is a method in an instance of class that doesn't work. So I need some ideas what should I do if sleeper function is an instant method (like below)
class SleepingClass:
def __init__(self, sec):
self.sec = sec
def sleeper(sec):
time.sleep(sec)
sleepingClass = SleepingClass(1)
if __name__ == "__main__":
sec=1
process = multiprocessing.Process(target=sleepingClass.sleeper,args=[sec])
process.start()
I tried running method not in instance class and it worked. But when I put it in instance class it did not.
The class wasn't written correctly. Comments below in code:
Output: