openpyxl线程安全吗? 我希望有一个线程更改工作表,而另一个线程定期保存。我想知道我是否需要在更改和保存操作周围添加一个锁定对象,或者它是否已经内置到 openpyxl 中。我没有在文档中看到任何内容,也没有看到任何关于 openpyxl 中线程的问题。
阅读您的回答后,我进行了以下测试:
import threading
import random
import time
from openpyxl import Workbook
wb = Workbook()
class openpyxlwriting ( threading.Thread):
def run ( self ):
global wb
ws = wb.get_active_sheet()
c = 1
for a in range(100):
for b in range(10000):
ws.cell(column = a,row=b).value = c
c += 1
print "row ",a
class openpyxlsaving ( threading.Thread):
def run ( self ):
global wb
for a in range(1000):
wb.save('test.xlsx')
print "saved"
openpyxlwriting().start()
time.sleep(1)
openpyxlsaving ().start()
这给了我以下回溯: trasback.png
最佳答案
"OpenPyXl is thread safe; Workbooks can be created concurrently by different threads. However, each workbook should only be modified by one thread at a time."
关于python - openpyxl线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11234203/