By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Return value of Thread

P: 3
Ziad
Dear all,

I want to get the return value of function(ziad_1) and function(ziad_2)???
Expand|Select|Wrap|Line Numbers
  1. import time
  2. import thread
  3.  
  4.  
  5.  
  6. def ziad_1(a):
  7.     all_i = []
  8.     print "Starte Unterprogramm ziad_1." 
  9.     for i in range (1, 5):
  10.         time.sleep(3)
  11.         i=a+i
  12.         print  "Thread 1: zaehler=", i 
  13.         all_i.append(i)
  14.         print "ziad_1"
  15.         print all_i
  16.     return all_i
  17.  
  18. def ziad_2(b):  
  19.     all_j = []
  20.     print "Starte Unterprogramm ziad_2." 
  21.     for j in range (1, 5):
  22.         time.sleep(3.1)
  23.         j=b+j
  24.         print  "Thread 2: zaehler=", j 
  25.         all_j.append(j)
  26.         print  "ziad_2"  
  27.         print all_j
  28.     return all_j
  29.  
  30. th1=thread.start_new_thread(ziad_1, (1,))
  31. th2=thread.start_new_thread(ziad_2, (1,))
#---------------

Thanks
Attached Images
File Type: jpg Aufzeichnen.jpg (68.1 KB, 147 views)
May 21 '11 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 621
The simplest solution is put everything in a class. And next time please use the "#" button at the top=code tags.
Expand|Select|Wrap|Line Numbers
  1. import time
  2. import threading
  3.  
  4.  
  5. class TestThread():
  6.     def __init__(self):
  7.         self.all_i = []
  8.         self.all_j = []
  9.  
  10.     def ziad_1(self, a):
  11.         print "Starte Unterprogramm ziad_1."
  12.         for i in range (1, 5):
  13.             time.sleep(3)
  14.             i=a+i
  15.             print "Thread 1: zaehler=", i
  16.             self.all_i.append(i)
  17.         print "ziad_1"
  18.         print self.all_i
  19.  
  20.     def ziad_2(self, b):
  21.         print "Starte Unterprogramm ziad_2."
  22.         for j in range (10, 15):
  23.             time.sleep(3.1)
  24.             j=b+j
  25.             print "Thread 2: zaehler=", j
  26.             self.all_j.append(j)
  27.         print "ziad_2"
  28.         print "all_j", self.all_j
  29.  
  30. TT=TestThread()
  31. th1=threading.Thread(target=TT.ziad_1, args=(1,))
  32. th1.start()
  33. th2=threading.Thread(target=TT.ziad_2, args=(1,))
  34. th2.start()
  35. th1.join()
  36. th2.join()
  37.  
  38. print "\nafter thread, all_i", TT.all_i
  39. print "after thread, all_j", TT.all_j 
May 21 '11 #2

P: 3
thanks man.

I found also another solution with Queue modul.
Attached Images
File Type: jpg Aufzeichnen.jpg (77.5 KB, 183 views)
May 21 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.