1

I am new to thread, I had encountered abnormal result while printing the value inside a list using thread to allow 2 functions to working at the same time and appending the result to a list. Below my code:

import threading
def func1():

  return "HTML"

def func2():

  return "IS FUN"

threadslist = []

thread1 = threading.Thread(target=func1)
thread2 = threading.Thread(target=func2)

x = thread1
y = thread2

x.start()
y.start()

threadslist.append(x)
threadslist.append(y)

print(threadslist)

And here is the result for the list:

[<Thread(Thread-1, stopped 1076)>, <Thread(Thread-2, stopped 7948)>]

Why it storing the Threads object instead of storing ['HTML', 'IS FUN'] ?

TYL
  • 83
  • 1
  • 1
  • 7
  • 1
    The thread isn't actually capturing the return value from each function, just executing it. – APorter1031 Nov 15 '17 at 14:36
  • 1
    You probably want to use a thread pool as seen here: https://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread-in-python#14299004 – APorter1031 Nov 15 '17 at 14:37
  • 1
    Possible duplicate of [how to get the return value from a thread in python?](https://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread-in-python) – internet_user Nov 15 '17 at 14:38

2 Answers2

2
import threading
threading_list = []
def func1():
   threading_list.append("HTML")

def func2():
   threading_list.append("IS FUN")

thread1 = threading.Thread(target=func1)
thread2 = threading.Thread(target=func2)

x = thread1
y = thread2

x.start()
y.start()

print(threading_list)
Sheshnath
  • 301
  • 3
  • 10
1

In your threadlist you are saving the Thread variables, so that is what you're seeing in your output is their representation as strings.

You can't get the return value of a function running in a different thread like that. To do what you can either:

  1. Use the multithreading module:

:

from multiprocessing.pool import ThreadPool
def func1():
  return 'HTML'

def func2():
  return 'IS FUN'


pool = ThreadPool(processes=1)

return_values = []

return_values.append(pool.apply(func1, ())) # Using apply for synchronous call directly returns the function return value.
func2_result = pool.applyasync(func2) # Using applyasync for asynchronous call will require a later call.

return_values.append(func2_result.get())  # get return value from asynchronous call to func2.

print(return_values)
  1. Use mutable object, like a list, to save the return values:

:

return_values = []
def func1():
   return_values.append('HTML')

def func2():
   return_values.append('IS FUN')

# rest of your code here
print(return_values)

And you'll get:

['HTML', 'IS FUN']

Tals
  • 468
  • 4
  • 17
  • 1
    Hi thank you for the solution, but for solution 1, can I use 1 pool instead of 2 pools? Because the solution got 2 pool = pool1 and pool2, thank you so much – TYL Nov 15 '17 at 15:03
  • 1
    Yes! And it's much more Pythonic. I updated my answer (Along with an `apply` example :) – Tals Nov 15 '17 at 15:18