Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

arrays - update python list of lists on multithreaded

I have this simple python code:

from threading import Lock, Condition, Thread

n_threads = 5
global_list = [[]]*n_threads

def update_list(t_id, lock):
    global global_list
    for i in range(t_id, t_id+4, 1):
        lock.acquire()
        global_list[t_id].append(i)
        lock.release()
    print('finishing', t_id)

all_threads = []
l = Lock()

for i in range(n_threads):
    c = Thread(name='name %s' % i, target=update_list, args=(i,l,))
    all_threads.append(c)
    c.start()

for my_thread in all_threads:
    my_thread.join()

Basically what I am trying to do is that each thread updates one of the lists and by the end of my code each list inside of the variable global_list will have elments from a single thread, like this:

global_list[0] = [0,1,2,3]
global_list[1] = [1,2,3,4]
...

Instead what I get is :

global_list[0] = [0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6, 4, 5, 6, 7]
global_list[1] = [0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6, 4, 5, 6, 7]
...

I am using a mutex to protect the operation, so basically I am clueless about why it wont work for lists in python and if threre is something I can use to achieve this.

question from:https://stackoverflow.com/questions/65905005/update-python-list-of-lists-on-multithreaded

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
for i in [[]]*5:
    print(id(i))

output

139972923894208
139972923894208
139972923894208
139972923894208
139972923894208

So, as you can see [[]]*5 doesn't create 5 lists, only one list.All other 4 are just representative of the first one. So when you do

list[0].append(1)

it makes the list as

[[1],[1],[1],[1],[1]]

try

global_list = [[], [], [], [], []]

or

global_list = [[] for i in range(n_threads)]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...