Python.NETを使用して複数のスレッドでセル値を同時に読み取る方法

Contents
[ ]

複数のスレッドで同時にセル値を読むには、worksheet.cells.multi_thread_readingTrueに設定します。設定しない場合、誤ったセル値を取得する可能性があります。

次のコード:

  1. ワークブックを作成
  2. シートを追加
  3. 文字列値でシートを埋める
  4. ランダムなセルから値を同時に読み取る2つのスレッドを作成 読み取った値が正しい場合、何も起こりません。読み取った値が間違っている場合は、メッセージが表示されます。

この行をコメントアウトすると、次のメッセージが表示されます:

test_workbook.worksheets[0].cells.multi_thread_reading = True

次のメッセージチェックがトリガーされます:

if s != f"R{row}C{col}":
    print("This message will show when cells read values are incorrect")

それ以外の場合、セルから読み取ったすべての値が正しい場合、プログラムはメッセージを表示せずに実行されます。

import threading
import random
import time
from aspose.cells import Workbook

test_workbook = None

def thread_loop():
    rand = random.Random()
    while True:
        try:
            row = rand.randint(0, 9999)
            col = rand.randint(0, 99)
            s = test_workbook.worksheets[0].cells.get(row, col).string_value
            if s != f"R{row}C{col}":
                print("This message will show up when cells read values are incorrect.")
        except:
            pass

def test_multi_threading_read():
    global test_workbook
    test_workbook = Workbook()
    test_workbook.worksheets.clear()
    test_workbook.worksheets.add("Sheet1")

    for row in range(10000):
        for col in range(100):
            test_workbook.worksheets[0].cells.get(row, col).value = f"R{row}C{col}"

    # Commenting this line will show a pop-up message
    # test_workbook.worksheets[0].cells.multi_thread_reading = True

    my_thread1 = threading.Thread(target=thread_loop, daemon=True)
    my_thread1.start()

    my_thread2 = threading.Thread(target=thread_loop, daemon=True)
    my_thread2.start()

    time.sleep(5)  # Sleep for 5 seconds

if __name__ == "__main__":
    test_multi_threading_read()