Is openpyxl thread safe?
I’m hoping to have one thread changing the worksheet while another thread saves at regular intervals. I was wondering if I needed to add a lock object around the changing and saving operations or whether that is already built in to openpyxl. I did not see anything in the documentation nor any previous questions about threads in openpyxl.
After reading your answer I ran the following test:
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()
which gave me the following trasback:
trasback.png
I decided to see if I could test this, so I created the simple program below that generates a bunch of threads that attempt to read and write the same cell, with random sleeps thrown in.
I didn’t see any OS errors that might indicate a problem. On the other hand, I did see some behavior that I didn’t quite understand. As the thread count increased, the number of loops completed decreased. So, for example, thread 100 only completed 8 passes through the loop. This might be some sort of an error on my part, or it might indicate an issue with thread safety.
Anyway, this should give you a start in testing it yourself, if you feel so inclined.