Adam_zampa
Adam_zampa

Reputation: 55

Return value from function in thread error

I have written a script in which a function calls 2 different functions together using Multi Threading.

def video_image(link_ID):
    threadp = ThreadPool(processes=1)
    res = threadp.apply_async(displayImage,( link_ID))
    return_val = res.get()
    tid = Thread(target=publishIAmFree)
    tid.start()
    if return_val == True:
        ............
        ............

def displayImage(link_ID):
    playFlag = True
    os.system("pkill feh")
    cmd = "feh /home/fa/Desktop/%s" % (link_ID)
    os.system(cmd)
    return playFlag

But, I am getting this error.

  File "C:/Users/user1/PycharmProjects/MediPlayerStructured/venv/Include/main_new_concept.py", line 281, in videoImageFunction
video_image(link_ID)
  File "C:/Users/user1/PycharmProjects/MediPlayerStructured/venv/Include/main_new_concept.py", line 349, in video_image
return_val = res.get()
  File "C:\Users\user1\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 657, in get
raise self._value
  File "C:\Users\user1\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 121, in worker
result = (True, func(*args, **kwds))
TypeError: displayImage() takes 1 positional argument but 23 were given

What is this error

TypeError: displayImage() takes 1 positional argument but 23 were given

and how do I solve this?

Upvotes: 1

Views: 49

Answers (1)

user2201041
user2201041

Reputation:

You're missing a comma. The simple way to reproduce this (in Python2) is

>>> apply(len, ('test'))
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
TypeError: len() takes exactly one argument (4 given)

>>> apply(len, ('test',))
4

The problem occurs because the args argument is treated as a sequence of arguments. I assume you're passing a string, which is a sequence, so each character becomes a separate argument.

Calling res = threadp.apply_async(displayImage, (link_ID, )) will let Python interpret it as you meant, with link_ID as the first element of a sequence.

Upvotes: 1

Related Questions