読者です 読者をやめる 読者になる 読者になる

Volo di notte

お勉強の成果メモや日常のこと

[Python] MultithreadingとPandas

DDE通信などIO待ち時間が大きい処理をPandasと組み合わせてマルチスレッド化。テンプレだけ作って解説は後日。

# -*- coding: utf-8 -*-
from queue import Queue
from threading import Thread
import pandas as pd

class TWorker(Thread):
    def __init__(self, queue, res):
        Thread.__init__(self)
        self.queue  = queue
        self.result = res

    def run(self):
        while True:
            arg0, arg1, arg2 = self.queue.get()
            
            # 処理
            res0  = arg0
            res1  = 1
            res2  = 2
            res3  = 3
            
            self.result.put([[res0, res1, res2, res3]])
            self.queue.task_done()

def MainTask():
    ColumnNames = ["COL0", "COL1", "COL2", "COL3"]

    df = pd.DataFrame([],columns = ColumnsOPTBL)
    queue  = Queue()
    result = Queue()

    for x in range(8):
        worker = TWorker(queue, result)
        worker.daemon = True
        worker.start()
    
    for param in [0, 1, 2]:
        queue.put((param, 1, 2))

    queue.join()
    while not result.empty() :
        res = result.get()
        df  = df.append(pd.DataFrame(res,columns = ColumnNames),ignore_index=True)
    print(df)

if __name__ == '__main__':
    MainTask()