如何访问从线程返回的值?

问题描述 投票:0回答:2

我是新使用Python 2.7.6,以。我需要他们开始后能得到我的线程返回的值。

我的代码如下:

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

该execute_client功能由每个线程执行。我想在年底每个线程value_back和存储他们让我们在列表中的数据结构说。执行功能是功能模块我写的里面。

我看着这个related issue但不知道如何去适应我的代码的答案。

python multithreading python-2.x
2个回答
2
投票

从您发布的链接通过,你应该做这样的事情:

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()

并通过获取结果:

fetch = myqueue.get()

0
投票

我现在解决了,谢谢你这么多球员。我所做的是如下:

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)

和它的工作。

问候你们。

© www.soinside.com 2019 - 2024. All rights reserved.