Visitar URL original
How To Get The Return Value From Function · VenkatAdd/PythonScript@55ad0fe · GitHub
Skip to content

Commit 55ad0fe

Browse files
authored
How To Get The Return Value From Function
How To Get The Return Value From Function
1 parent 075c5a9 commit 55ad0fe

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
##------------------------------------------------------------------------------------------------------
2+
#Created By:Ravishankar Chavare
3+
#version:python 3.7
4+
#Date:10/04/2019
5+
#File_des:How to get Return value from running Thread
6+
##------------------------------------------------------------------------------------------------------
7+
8+
9+
'''
10+
To get Return value From thread function we can use queue
11+
12+
The queue module implements multi-producer, multi-consumer queues.
13+
It is especially useful in threaded programming when information must be exchanged safely between multiple threads
14+
'''
15+
import queue #import queue
16+
from threading import Thread #import Threading
17+
18+
#Simple Function retrun the value
19+
def foo(val):
20+
print('I am From Foo Function and val={0}'.format(val))
21+
return val*val
22+
23+
#Create a Blank Queue for adding thread
24+
que = queue.Queue()
25+
26+
#Create a anonymous function using lambda that will call foo() and and add that calling method to queue
27+
#for further processing
28+
fn=lambda q,arg1:q.put(foo(arg1))
29+
30+
#Now Normal Create A Thread using Threading.Thread(target=anomous function,args=(que,elements))
31+
t = Thread(target=fn, args=(que, 5))
32+
33+
#Start That Thread
34+
t.start()
35+
36+
#WAit Untill to Thread Execution
37+
t.join()
38+
39+
#Now Main Part to get the result from foo Function call que.get() to get the return value
40+
result = que.get()
41+
42+
#Print The Result Which is stored using que.get()
43+
print(result)

0 commit comments

Comments
 (0)