简体   繁体   中英

Pass-by-reference mutable variable in a thread

I have a basic question about multi-threading in Python: I have a list and I need modify this in a thread. I understand that an list is a mutable type: How do I pass a variable by reference?

But, when I using thread the list is not behaving like a mutable type:

from multiprocessing import Process, Lock

def f(l, i, n):
    l.acquire()
    i.append(n)
    l.release()
    print "in:", i

if __name__ == '__main__':
    lock = Lock()

    i = []
    for num in range(10):
        p = Process(target=f, args=(lock, i, num))
        p.start()
        p.join()

    print "out:", i

output

in: [0]
in: [1]
in: [2]
in: [3]
in: [4]
in: [5]
in: [6]
in: [7]
in: [8]
in: [9]
out: []

Can someone please help me with this issue?

The code is not using threads, but processes which does not share memory.

Use threads:

from threading import Thread, Lock  # <----

def f(l, i, n):
    l.acquire()
    i.append(n)
    l.release()
    print "in:", i

if __name__ == '__main__':
    lock = Lock()

    i = []
    for num in range(10):
        p = Thread(target=f, args=(lock, i, num))  # <----
        p.start()
        p.join()

    print "out:", i

You need to join on a seperate loop. By having the join in the same loop, it forces you to wait for the thread to complete before kicking off the next thread. See code below...

 from threading import Thread, Lock # <---- def f(l, i, n): l.acquire() i.append(n) l.release() print "in:", i if __name__ == '__main__': lock = Lock() i = [] for num in range(10): p = Thread(target=f, args=(lock, i, num)) # <---- p.start() for num in range(10): p.join() print "out:", i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM