2

I am quite new to python and probably facing a very simple problem. However, I was not able to find a solution via Google, as the information I found indicated my method should work.

All I want to do is passing in an array as argument to a function.

The function that shall take an array:

def load(components):
    global status
    global numberOfLoadedPremixables
    results = []
    print('componetnsJson: ', componentsJson, file=sys.stderr)
    status = statusConstants.LOADING

    for x in range(0, len(components)):
        blink(3)
        gramm = components[x]['Gramm']

        #outcome = load(gramm)
        time.sleep(10)
        outcome = 3
        results.append(outcome)
        numberOfLoadedPremixables += 1

    status = statusConstants.LOADING_FINISHED

Then I am trying to start this function on a background thread:

background_thread = threading.Thread(target=load, args=[1,2,3]) #[1,2,3] only for testing
background_thread.start()

As a result, I end up with the error:

TypeError: load() takes 1 positional argument but 3 were given

1 Answer 1

2

Since you need to pass the whole array as a single unit to the function, wrap that in a tuple:

background_thread = threading.Thread(target=load, args=([1,2,3],))

The (,) turns the args into a single-element tuple that gets passed to your function

The issue is happening because python expects args to be a sequence which gets unwrapped when being passed to the function, so your function was actually being called like: load(1, 2, 3)

Sign up to request clarification or add additional context in comments.

1 Comment

Solved my problem! Thanks a lot! Will accept the answer in 9 minute ;-)

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.