我目前正在嘗試運行此代碼:
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
我的圖書館似乎有問題,我從未編輯或打開任何圖書館,我該如何解決?
uj5u.com熱心網友回復:
根據此https://docs.python.org/3/library/threading.html#threading.Thread.run,目標可呼叫物件被呼叫Thread.run并傳遞您在 arg 和 kwarg 引數中提供的引數。因此,正如您提供的那樣,args=(thread_number,)您應該像這樣定義您的訪問功能
def access(thread_number):
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/430559.html
上一篇:執行緒會提高這個回圈的速度嗎?
