问题是: 我想创建一个程序,使用单个线程添加成对的数字。 代码如下:
import threading
from queue import Queue
print_lock = threading.Lock()
q = Queue()
numbers = [[235465645, 4345464565], [52546546546, 433453435234], [1397675464, 5321453657], [980875673, 831345465], [120938234, 289137856], [93249823837, 32874982837]]
def addition(pair):
num1 = pair[1]
num2 = pair[2]
total = num1 + num2
with print_lock:
print(num1, '+', num2, ':', total)
def threader():
while True:
pair = numbers.pop(0)
calculator = q.get()
addition(pair)
q.task_done()
for i in range(len(numbers)):
t = threading.Thread(target = threader)
t.daemon = True
t.start()
for i in range(len(numbers)):
q.put(i)
q.join()
但是当我运行程序时,我得到的只是两个空行。我不知道是什么问题。我使用的是3.4版,如果有任何帮助的话
我将非常感谢任何帮助。 非常感谢。 穆阿萨西姆·穆罕默德P
在…中断开索引:
Python从
0
索引,因此len(pair)
为2时,pair[2]
使用IndexError
终止线程。最佳:因此,您甚至不必回忆关于Python索引的非常重要的细节,只需将2项序列解压为两个标量,然后,就可以了!)
相关问题 更多 >
编程相关推荐