1

The most basic form of something I want to code is the code as follows:

import threading

arr = []
def test(id):
    global arr
    arr.append(id)

threading.Thread(target=test, args="8")
print(arr)

What I want to do is to append "8" to a global variable called arr But this doesn't happen, and print(arr) gives this output:

[]

However, if I use this code, everything works fine:

import threading

arr = []
def test(id):
    global arr
    arr.append(id)

test("8")
print(arr)

The problem seems to be with thread, so how can I use thread and also change the value of a global variable inside the function test?

1 Answer 1

5

You also have to start the thread to actually run the function test

import threading

arr = []
def test(id):
    global arr
    arr.append(id)

t = threading.Thread(target=test, args="8")
t.start()
t.join()
print(arr)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.