线程打印空 space

threads printing empty space

问题是这样的: 我想创建一个程序来使用单独的线程添加成对的数字。 这是代码:

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

索引损坏...:[=​​16=]

def addition(pair):
    num1 = pair[1]
    num2 = pair[2]
    (etc)

Python 来自 0 的索引,因此 len(pair) 为 2,pair[2] 使用 IndexError 终止线程。最佳:

def addition(pair):
    num1, num2 = pair
    (etc)

所以您甚至不必回忆关于 Python 索引的相当重要的细节——您只需将 2 项序列解压缩为两个标量,然后,开始吧!-)