Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 using locks in multithreading in python3
#1
import threading
 
 
class Class1(threading.Thread):
    def __init__(self, i, j):
        self.i = i
        self.j = j
 
    def f1(self):
       i = self.i
       global lock
       lock.acquire()
       while i<=100:
           print(" I is now %d \n" %i)
           i=i+1
           lock.release()
        
    def f2(self):
        j = self.j
        global lock
        lock.acquire()
        while j<=50:
            print("J is now %d \n" %j)
            j=j+1
            lock.release()
         
    def Main1(self):
        lock = threading.Lock()
        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()


Getting error in the code
Error:
Exception in thread Thread-2: Traceback (most recent call last): File "C:\Python37\lib\threading.py", line 917, in _bootstrap_inner self.run() File "C:\Python37\lib\threading.py", line 865, in run self._target(*self._args, **self._kwargs) File "D:\Python\PythonExamples\Multithreading\SyncThreadingPython.py", line 23, in f2 lock.acquire() NameError: name 'lock' is not defined
Quote
#2
Move line #28 somewhere outside the class definition, e.g. to the line #2. Global statements not necessary here.
Quote
#3
Hi,

couple of remarks on the code:
  • Don't use global. In 99% of all cases it's a clear indication that the design pattern of your code is not good. global can be avoided in most cases.
  • In case you need a lock within your class, make it an attribute of you class.
  • The lock doesn't make sense here anyway at all, as there is no concurrent operation on anything.
  • Your are using a very old school way of string formating... Use the string's format() method or for Python >= 3.6 f-strings.

Regards, noisefloor
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Error in implementing multithreading in a class srm 2 142 May-16-2019, 03:54 PM
Last Post: Yoriz
  what does "python3" mean windows 3 216 Mar-17-2019, 10:14 AM
Last Post: metulburr
  Queue get memory leak when used in multithreading wangcp 1 666 Nov-27-2018, 04:06 AM
Last Post: wangcp
  utf-8 in python3 Skaperen 2 666 May-18-2018, 06:55 AM
Last Post: Skaperen
  Multithreading with queues - code optimization h1v3s3c 1 668 May-10-2018, 10:40 AM
Last Post: ThiefOfTime
  How to reallocating memory space in multithreading program in python ? siva 2 935 Mar-08-2018, 03:49 AM
Last Post: siva
  Another working code, help required for faster multithreading execution anna 0 655 Feb-09-2018, 03:26 AM
Last Post: anna
  python3 on Mac OS X wyattbiker 3 1,661 Oct-31-2017, 03:51 PM
Last Post: snippsat
  Multithreading in a loop valtih 3 6,296 Aug-03-2017, 08:20 PM
Last Post: valtih
  Multithreading with ssh connection harusin 6 1,970 Jul-10-2017, 11:40 AM
Last Post: harusin

Forum Jump:


Users browsing this thread: 1 Guest(s)