Error in implementing multithreading in a class - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Error in implementing multithreading in a class (/thread-18409.html) |
Error in implementing multithreading in a class - srm - May-16-2019 Hello, Just started learning multi threading concept in python as beginner..I want to print values 1 to 10 with two different functions using multithreading concept within a class ,my code is below: import threading class Class1: i=1 j=1 def f1(): global i while i<=10: print(" I is now %d" %i) i=i+1 def f2(): global j while j<=10: print("J is now %d" %j) j=j+1 def Main1(): t1=threading.Thread(target=f1()) t2=threading.Thread(target=f2()) t1.start() t2.start() ob=Class1() ob.Main1()But getting error:
RE: Error in implementing multithreading in a class - Larz60+ - May-16-2019 Write it this way: import threading class Class1: def __init__(self, i, j): self.i = i self.j = j def f1(self): i = self.i while i<=10: print(f'I is now {i}') # print(" I is now %d" %i) i=i+1 def f2(self): j = self.j while j<=10: print("J is now %d" %j) j=j+1 def Main1(self): t1=threading.Thread(target=self.f1()) t2=threading.Thread(target=self.f2()) t1.start() t2.start() if __name__ == '__main__': ob=Class1(1,1) ob.Main1()output:
RE: Error in implementing multithreading in a class - Yoriz - May-16-2019 The () needs removing from the target=self.f1 & target=self.f2 for the thread to call them.
|