Home > database >  Python package problem while trying to use threading
Python package problem while trying to use threading

Time:02-22

I am currently trying to run this code:

import threading
import time


semafor = threading.BoundedSemaphore(value=5)

def access():
    print("{} wants to have permission".format(thread_number))
    semafor.acquire()
    print("{} got permission.".format(thread_number))
    time.sleep(5)
    print("{} lost permission.".format(thread_number))
    semafor.release()


for thread_number in range(1,11):
    t = threading.Thread(target=access, args=(thread_number,))
    t.start()
    time.sleep(1) 

and I'm getting this error:

>Traceback (most recent call last):
  File "C:\x\x\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "C:\x\x\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
TypeError: access() takes 0 positional arguments but 1 was given

It seems like there is a problem in my library, I never edited or opened any of the libraries, how can i fix it?

CodePudding user response:

According to this https://docs.python.org/3/library/threading.html#threading.Thread.run , the target callable is called by Thread.run and it passes the arguments you provided in the arg and kwarg parameters. So as you provided args=(thread_number,) you should define you access function like this

def access(thread_number):
  • Related