Python Forum
Error in implementing multithreading in a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in implementing multithreading in a class
#1
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:
Error:
Traceback (most recent call last): File "C:/Threading1.py", line 26, in <module> ob.Main1() TypeError: Main1() takes 0 positional arguments but 1 was given
Reply
#2
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:
Output:
I is now 1 I is now 2 I is now 3 I is now 4 I is now 5 I is now 6 I is now 7 I is now 8 I is now 9 I is now 10 J is now 1 J is now 2 J is now 3 J is now 4 J is now 5 J is now 6 J is now 7 J is now 8 J is now 9 J is now 10
Reply
#3
The () needs removing from the target=self.f1 & target=self.f2 for the thread to call them.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  multithreading Hanyx 4 1,282 Jul-29-2022, 07:28 AM
Last Post: Larz60+
Question Problems with variables in multithreading Wombaz 2 1,286 Mar-08-2022, 03:32 PM
Last Post: Wombaz
  Multithreading question amadeok 0 1,746 Oct-17-2020, 12:54 PM
Last Post: amadeok
  error in class non_name092 1 1,859 Sep-02-2020, 05:42 PM
Last Post: bowlofred
  How can i add multithreading in this example WoodyWoodpecker1 3 2,448 Aug-11-2020, 05:30 PM
Last Post: deanhystad
  matplotlib multithreading catosp 0 2,906 Jul-03-2020, 09:33 AM
Last Post: catosp
  Problem by implementing IDF moat335 0 1,497 Apr-30-2020, 11:33 AM
Last Post: moat335
  Multithreading dynamically syncronism Rodrigo 0 1,502 Nov-08-2019, 02:33 AM
Last Post: Rodrigo
  Locks in Multithreading Chuonon 0 1,815 Oct-03-2019, 04:16 PM
Last Post: Chuonon
  Implementing OAuth 2 ( 2-legged) krishnanunnik 2 2,111 Sep-16-2019, 11:13 AM
Last Post: krishnanunnik

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020