连接python线程,总计

连接python线程,总计,python,multithreading,Python,Multithreading,我的问题是我试图循环一个包含多个线程的数组,然后每个线程将值添加到全局数组中。但由于某种原因,数组按计划推出,我注意到您需要使用join()线程函数,但对于如何在这里实现它,我有点困惑 totalPoints = [] def workThread(i): global totalPoints totalPoints += i threads = [] for i in range(NUMBER_OF_THREADS): t = threading.Thread(t

我的问题是我试图循环一个包含多个线程的数组,然后每个线程将值添加到全局数组中。但由于某种原因,数组按计划推出,我注意到您需要使用join()线程函数,但对于如何在这里实现它,我有点困惑

totalPoints = []

def workThread(i):
    global totalPoints
    totalPoints += i


threads = []
for i in range(NUMBER_OF_THREADS):
    t = threading.Thread(target=workThread, args=(i,))
    t.start()
    threads.append(t)

任何帮助都将不胜感激。谢谢

您只需编写第二个循环来加入线程

totalPoints = []

def workThread(i):
    global totalPoints
    totalPoints += i

threads = []
for i in range(NUMBER_OF_THREADS):
    t = threading.Thread(target=workThread, args=(i,))
    t.start()
    threads.append(t)
for t in threads:
    t.join()

您的代码将在
totalPoints+=i
时失败,因为
totalPoints
是一个列表。您不处理线程中的异常,因此您可能会无声地失败,而不知道发生了什么。此外,您需要注意如何访问共享资源,如
totalPoints
,以确保线程安全。

您只需编写第二个循环来加入线程

totalPoints = []

def workThread(i):
    global totalPoints
    totalPoints += i

threads = []
for i in range(NUMBER_OF_THREADS):
    t = threading.Thread(target=workThread, args=(i,))
    t.start()
    threads.append(t)
for t in threads:
    t.join()
您的代码将在
totalPoints+=i
时失败,因为
totalPoints
是一个列表。您不处理线程中的异常,因此您可能会无声地失败,而不知道发生了什么。此外,您需要注意如何访问共享资源,如
totalPoints
,以实现线程安全。

这有帮助吗:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import threading
import time
import random

NUMBER_OF_THREADS=20

totalPoints = []

def workThread(i):
    global totalPoints
    time.sleep(random.randint(0, 5))
    totalPoints.append((i, random.randint(0, 255)))

threads = []
for i in range(NUMBER_OF_THREADS):
    t = threading.Thread(target=workThread, args=(i,))
    t.start()
    threads.append(t)

for t in threads:
  t.join()
print totalPoints
它总是打印如下内容:

[(1, 126), (10, 169), (11, 154), (0, 214), (9, 243), (12, 13), (15, 152), (6, 24), (17, 238), (13, 28), (19, 78), (16, 130), (2, 110), (3, 186), (8, 55), (14, 70), (5, 35), (4, 39), (7, 11), (18, 14)]
还是这个

[(2, 132), (3, 53), (4, 15), (6, 84), (8, 223), (12, 39), (14, 220), (0, 128), (9, 244), (13, 80), (19, 99), (7, 184), (11, 232), (17, 191), (18, 207), (1, 177), (5, 186), (16, 63), (15, 179), (10, 143)]
这有帮助吗:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import threading
import time
import random

NUMBER_OF_THREADS=20

totalPoints = []

def workThread(i):
    global totalPoints
    time.sleep(random.randint(0, 5))
    totalPoints.append((i, random.randint(0, 255)))

threads = []
for i in range(NUMBER_OF_THREADS):
    t = threading.Thread(target=workThread, args=(i,))
    t.start()
    threads.append(t)

for t in threads:
  t.join()
print totalPoints
它总是打印如下内容:

[(1, 126), (10, 169), (11, 154), (0, 214), (9, 243), (12, 13), (15, 152), (6, 24), (17, 238), (13, 28), (19, 78), (16, 130), (2, 110), (3, 186), (8, 55), (14, 70), (5, 35), (4, 39), (7, 11), (18, 14)]
还是这个

[(2, 132), (3, 53), (4, 15), (6, 84), (8, 223), (12, 39), (14, 220), (0, 128), (9, 244), (13, 80), (19, 99), (7, 184), (11, 232), (17, 191), (18, 207), (1, 177), (5, 186), (16, 63), (15, 179), (10, 143)]