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

Top Page

Messages In This Thread
using locks in multithreading in python3 - by srm - Jul-13-2019, 07:08 AM

Possibly Related Threads...
Thread Author Replies Views Last Post
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 91 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  Locks in Multithreading Chuonon 0 96 Oct-03-2019, 04:16 PM
Last Post: Chuonon
  Multithreading alternative MartinV279 1 178 Aug-01-2019, 11:41 PM
Last Post: scidam
  Multithreading in a loop valtih 3 7,523 Aug-03-2017, 08:20 PM
Last Post: valtih
  Multithreading with ssh connection harusin 6 2,221 Jul-10-2017, 11:40 AM
Last Post: harusin

Forum Jump:


Users browsing this thread: 1 Guest(s)