【发布时间】:2021-08-18 13:00:02
【问题描述】:
在我从连接到打印机的条形码扫描仪获取输入的应用程序中,我想根据正在扫描的 ID 标记已打印的文档。
由于网络可以很慢,打印机可以很快,所以我想避免请求没有完成,程序在扫描下一个文档时无法接受新输入的情况。
我试图用线程解决这个问题,但在这一点上卡住了。我得到的只是错误:TypeError: updateOrder() missing 1 required positional argument: 'ids'
这里是完整的代码:
import requests
from requests.models import HTTPBasicAuth
import threading
# Empty Array to buffer the incoming billbeeIds
ids = []
# Get the order-id from the scanner
def getInput():
while True:
newId = str(input("BillBeeId eingeben"))
ids.append(newId)
# Mark order-id as printed
def updateOrder(ids):
while len(ids) > 0:
billbeeId = ids.pop(0)
response = requests.post(
'https://app.billbee.io/api/v1/orders/{0}/tags'.format(billbeeId),
auth=HTTPBasicAuth('xxx', 'xxx'),
headers={'X-Billbee-Api-Key': 'xxx'},
json={
"Tags": [
"Lieferschein-Gedruckt"
]
}
)
print(response)
t1 = threading.Thread(target=getInput)
t2 = threading.Thread(target=updateOrder, args=ids)
t1.start()
t2.start()
【问题讨论】:
-
您收到错误是因为
args应该是一个参数列表或元组,而您的一开始是空的。您可以通过调用t2 = threading.Thread(target=updateOrder, args=[ids])来消除此错误。 -
谢谢,有道理!
标签: python multithreading