0

I'm new to using Python 2.7.6. I need to get the values returned by my threads after starting them.

My code is as follow :

from threading import Thread
from functions import *

class Client(Thread):
    def __init__(self, index,ip,t,i=1):
        Thread.__init__(self)
        self.ip_dst = ip
        self.t = t

    def run(self):
        value_back = execute(self.ip_dst,self.t,i=1)
        return value_back

The execute_client function is executed by every Thread. I want to get the value_back for each thread at the end and store them let's say in a list data structure. The execute function is inside the functions module I wrote.

I looked at this related issue but did not understand how to adapt the answer for my code.

martineau
  • 119,623
  • 25
  • 170
  • 301
user2567806
  • 460
  • 3
  • 7
  • 17

2 Answers2

2

Adopted from the link you posted, you should do something like this:

from Queue import Queue
from threading import Thread
from functions import *

class Client(Thread):
    def __init__(self, someargs, queue):
        Thread.__init__(self)
        self.queue = queue

    def run(self):
        value_back = execute(self.ip_dst,self.t,i=1)
        self.queue.put(value_back)

myqueue = Queue.Queue
myclient = Client(someargs, myqueue)
myclient.start()

And fetch the results via:

fetch = myqueue.get()
  • Thank you so much. But do I need to do myclient.join() or not ? – user2567806 Jun 28 '15 at 17:48
  • @user2567806 See this https://docs.python.org/2/library/threading.html#thread-objects (section Join): joining the thread makes the base thread pause until myclient has terminated. Like this, the program stops, when every thread finished execution. –  Jun 28 '15 at 17:56
  • So basically, yes, you should, at the end of the program. –  Jun 28 '15 at 17:58
  • This is the error I've got when I added myclient.join(): RuntimeError: cannot join thread before it is started. So I conclude that when we call directly the run method we do not need to do a join, right ? Because I did not use start() method for the thread. – user2567806 Jun 28 '15 at 18:08
  • I executed the code and surprisingly the Threads are not started concurrently ^-^. They are executed one after one. I do not understand Why, is that because of I call run() method instead of start () ? – user2567806 Jun 28 '15 at 18:14
  • Yes, sorry, my fault. You need to call start() which then calls the run() method itself. Like this, the run() method is started in a separate thread –  Jun 28 '15 at 18:22
  • As you cannot pass arguments through the start method to the run method, you should pass the queue to the constructor. Gonna edit my answer... –  Jun 28 '15 at 18:24
0

I resolved it now, thank you so much guys. What I did is as follows :

from Queue import Queue

from threading import Thread

from functions import *

class Client(Thread): def __init__(self,myqueue): Thread.__init__(self) self.myqueue = myqueue def run(self): value_back = execute(self.ip_dst,self.t,i=1) self.myqueue.put(value_back)

And it worked.

Regards guys.

user2567806
  • 460
  • 3
  • 7
  • 17