Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python线程运行函数返回值_Python 3.x_Pandas_Multithreading_Dataframe - Fatal编程技术网

Python 3.x Python线程运行函数返回值

Python 3.x Python线程运行函数返回值,python-3.x,pandas,multithreading,dataframe,Python 3.x,Pandas,Multithreading,Dataframe,我有pandasDataframes,我想在上面应用一个函数。我希望有很多迭代,所以我认为使用多个线程将非常好 这就是它的样子: def my_function(data_inputs_train): #..... do something with dataframe.... #..... group by for loops etc ....... #..... create new dataframe..... return newPandasDataFr

我有pandasDataframes,我想在上面应用一个函数。我希望有很多迭代,所以我认为使用多个
线程将非常好

这就是它的样子:

def my_function(data_inputs_train):

    #..... do something with dataframe....
    #..... group by for loops etc .......
    #..... create new dataframe.....
    return newPandasDataFrame

class myThread (threading.Thread):

   def __init__(self, threadID, data_inputs_train):

      threading.Thread.__init__(self)

      self.threadID = threadID
      self.data_inputs_train = data_inputs_train

   def run(self):
      result_df = my_function(data_inputs_train)

thread1 = myThread(1, data_inputs_train)
thread2 = myThread(2, data_inputs_train)
因此,两个线程都应该返回一个新的数据帧,在两个线程都完成后,我想连接从两个线程返回的两个结果

我该怎么做?如何从
run()
函数返回任何对象,以及如何在
thread1
对象中访问它

谢谢大家!

按第一个答案更新,但不起作用,也存在缩进问题。

class myThread (threading.Thread):

   def __init__(self, threadID, name, sleep, cust_type, data_inputs_train):

      threading.Thread.__init__(self)

      self.threadID = threadID
      self.name = name
      self.sleep = sleep
      self.cust_type = cust_type
      self.data_inputs_train = data_inputs_train
      #here i need to get the newPandasDataFrame object.
      result_df = fdp.optimze_score_and_cl(data_inputs_train)

    def returnTheData(self):
        return result_df

所以这是你计划的基础。。我只是使用示例数据来说明如何设置它

    def myFunction(x):
       df = pd.DataFrame(['1', '2'], columns = ['A'])
       return df

    class myThreads(threading.Thread):
        def __init__(self, threadID, name, sleep, cust_type, data_inputs_train):
           threading.Thread.__init__(self)
           self.threadID = threadID
           self.name = name
           self.sleep = sleep
           self.cust_type = cust_type

           # call the methods you need on your data...
           self.data_inputs_train = myFunction(data_inputs_train)

         def returnTheData(self):
           return self.data_inputs_train


df = pd.DataFrame(['1'], columns = ['A'])
thread1 = myThreads(1, "EX1", 1, 'EX', df)
thread2 = myThreads(2, "IN1", 2, 'IN', df)
thread1.start()
thread2.start()

thread1.join()
thread2.join()

df1 = thread1.returnTheData()
df2 = thread2.returnTheData()

print(df1)
print(df2)
你声明你的线程。。启动它们,基本上让它们运行任何你想要的

加入

允许主函数等待所有线程完成其处理

df2=thread2.returnTheData()

您只需调用一个函数来返回所需的数据

工作守则


对不起,不清楚。。你的答案中没有run()是不是有问题?//对数据调用所需的方法。。。在“初始化”下,您可以调用对数据有效的任何函数。这样不好:(对不起,我的函数在我的类之外。我无法在线程的init函数中调用它。你可能误解了我。我不想返回原始数据帧。我在线程中调用的函数将在我操作原始数据帧后创建另一个数据帧对象。是的,我认为我的答案仍然有效。让我将其编辑为。)模仿你的程序