How to get the return value from a thread using python

0 votes

How to get the value 'foo' which is returned from the thread as shown below?

from threading import Thread

def foo(bar):
    print 'hello {}'.format(bar)
    return 'foo'

thread = Thread(target=foo, args=('world!',))
thread.start()
ret = thread.join()
print ret

 Returns None in the above case.

Thanks for the help!

Dec 3, 2018 in Python by Anirudh
• 2,080 points

edited Dec 19, 2018 by Anirudh 100,182 views
Hey there, I'm completely new here, Now i am not sure if this section certainly is the right place to post this and sorry with this, but I had been hoping a friend here on edureka.co would be able to guide me.
Now i am wondering anybody knows just about any trusted reference for bitcoin signals. Is this website good  and anyone worked with them ?
<a href=https://signalforall.com/>daily crypto signals</a>
Likewise please introduce any good as well as comprehensive directory site for more inormation about this sort of services. We appreciate it.
Enjoy daily galleries
http://lompico.vintage.porn.moesexy.com/?reese
 harry potter comic porn men on men porn free porn lisa ann big boobs porn tube wet watch free porn movies hosted
Hey there, I'm new here, Now i am not sure in case this section would be the right place to create this and also sorry for this, but We were hoping a person here on edureka.co would be able to assist me.
Now i am wondering if anyone knows any trusted reference for daily crypto signals. Is this website trusted  and anyone worked with them ?
<a href=https://signalforall.com/>crypto signals</a>
Likewise please bring in any good and comprehensive directory for more inormation about these kinds of services. I actually appreciate it.

3 answers to this question.

0 votes

You don't need to change your existing code:

import Queue
from threading import Thread

def foo(bar):
    print 'hello {0}'.format(bar)
    return 'foo'

que = Queue.Queue()

t = Thread(target=lambda q, arg1: q.put(foo(arg1)), args=(que, 'world!'))
t.start()
t.join()
result = que.get()
print result

It can be also easily adjusted to a multi-threaded environment as well. Check this below:

import Queue
from threading import Thread

def foo(bar):
    print 'hello {0}'.format(bar)
    return 'foo'

que = Queue.Queue()
threads_list = list()

t = Thread(target=lambda q, arg1: q.put(foo(arg1)), args=(que, 'world!'))
t.start()
threads_list.append(t)

# Add more threads here
...
threads_list.append(t2)
...
threads_list.append(t3)
...

# Join all the threads
for t in threads_list:
    t.join()

# Check thread's return value
while not que.empty():
    result = que.get()
    print result

Hope this helps, 

If you are a beginner and need to know more about Python, It's recommended to go for Python Certification course today.

Thanks!

answered Dec 3, 2018 by Nymeria
• 3,560 points

edited Dec 19, 2018 by Nymeria

What does 

lambda q, 

Do?

Hey @Ali,

Lambda function always declared with no name but which can have any number of arguments but one expression. Here 'lambda q' is the function where 'q' is the argument and will get evaluated and returned.

Thanks a lot...Your answer is just awesome :)
Very beautiful answer that helps to understand.

I want to return something when the thread is alive. How can I do that?

Thanks in advance

Hi, @Shail,

Thank you for your contribution to the Edureka community, but whatever you want to return, you can return here.

0 votes

In Python 3.2+, stdlib concurrent. futures module provides a higher-level API to threading, including passing return values or exceptions from a worker thread back to the main thread:

import concurrent.futures

def foo(bar):
    print('hello {}'.format(bar))
    return 'foo'

with concurrent.futures.ThreadPoolExecutor() as executor:
    future = executor.submit(foo, 'world!')
    return_value = future.result()
    print(return_value)
answered Dec 15, 2020 by Gitika
• 65,890 points
0 votes

FWIW, the multiprocessing module has a nice interface for this using the Pool class. And if you want to stick with threads rather than processes, you can just use the multiprocessing.pool.ThreadPool class as a drop-in replacement.

def foo(bar, baz):
  print 'hello {0}'.format(bar)
  return 'foo' + baz

from multiprocessing.pool import ThreadPool
pool = ThreadPool(processes=1)

async_result = pool.apply_async(foo, ('world', 'foo')) # tuple of args for foo

# do some other stuff in the main process

return_val = async_result.get()  # get the return value from your function.
answered Dec 15, 2020 by Roshni
• 10,520 points

Related Questions In Python

0 votes
1 answer

How to get the latest file in a folder using python?

Hello @kartik,  would suggest using glob.iglob() instead of the glob.glob(), as ...READ MORE

answered May 27, 2020 in Python by Niroj
• 82,820 points
9,765 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,450 points
713 views
0 votes
1 answer

How to get text label from SAP using pywinauto[python]

Hi. Can you please tell me what ...READ MORE

answered Jun 28, 2018 in Python by Nietzsche's daemon
• 4,260 points
1,610 views
0 votes
1 answer

How to correctly return an a dictionary as an output in zappier code using python?

David here, from the Zapier Platform team. ...READ MORE

answered Dec 3, 2018 in Python by charlie_brown
• 7,720 points
953 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,270 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
2,468 views
0 votes
1 answer

How to use read a WSDL file from the file system using Python suds?

Hi, good question. It is a very simple ...READ MORE

answered Jan 21, 2019 in Python by Nymeria
• 3,560 points
6,375 views
0 votes
2 answers

How to add a certain time delay to the code using Python?

You can use time.sleep(duration in second) READ MORE

answered Feb 14, 2019 in Python by Shashank
• 1,370 points
504 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP