Python 尝试使用线程时出现包问题
Python package problem while trying to use threading
我目前正在尝试 运行 此代码:
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)
我收到了这个错误:
>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
我的库似乎有问题,我从未编辑或打开过任何库,我该如何解决?
根据此 https://docs.python.org/3/library/threading.html#threading.Thread.run ,目标可调用对象由 Thread.run
调用并传递您在 arg 和 kwarg 参数中提供的参数。
因此,当您提供 args=(thread_number,)
时,您应该像这样定义访问函数
def access(thread_number):
我目前正在尝试 运行 此代码:
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)
我收到了这个错误:
>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
我的库似乎有问题,我从未编辑或打开过任何库,我该如何解决?
根据此 https://docs.python.org/3/library/threading.html#threading.Thread.run ,目标可调用对象由 Thread.run
调用并传递您在 arg 和 kwarg 参数中提供的参数。
因此,当您提供 args=(thread_number,)
时,您应该像这样定义访问函数
def access(thread_number):