[英]Multiple threads writing to the same CSV in Python
我是 Python 多线程的新手,目前正在编写一个附加到 csv 文件的脚本。 如果我要将多个线程提交给将行附加到 csv 文件的concurrent.futures.ThreadPoolExecutor
。 如果追加是这些线程执行的唯一与文件相关的操作,我可以做些什么来保证线程安全?
我的代码的简化版本:
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
for count,ad_id in enumerate(advertisers):
downloadFutures.append(executor.submit(downloadThread, arguments.....))
time.sleep(random.randint(1,3))
我的线程类是:
def downloadThread(arguments......):
#Some code.....
writer.writerow(re.split(',', line.decode()))
我应该设置一个单独的单线程执行器来处理写入,还是我只是在附加?
编辑:我应该详细说明,在下一次附加文件之间的几分钟内,发生写入操作的时间可能会有很大差异,我只是担心在测试我的脚本时没有发生这种情况,我希望对此进行介绍。
我不确定csvwriter
是否是线程安全的。 文档没有指定,所以为了安全起见,如果多个线程使用同一个对象,你应该使用threading.Lock
来保护使用:
# create the lock
import threading
csv_writer_lock = threading.Lock()
def downloadThread(arguments......):
# pass csv_writer_lock somehow
# Note: use csv_writer_lock on *any* access
# Some code.....
with csv_writer_lock:
writer.writerow(re.split(',', line.decode()))
话虽如此, downloadThread
将写任务提交给执行器确实可能更优雅,而不是像这样显式使用锁。
Way-late-to-the-party 注意:您可以通过让单个写入器从共享队列中消费,并通过执行处理的线程将行推送到队列来以不同的方式处理此问题,而无需锁定。
from threading import Thread
from queue import Queue
from concurrent.futures import ThreadPoolExecutor
# CSV writer setup goes here
queue = Queue()
def consume():
while True:
if not queue.empty():
i = queue.get()
# Row comes out of queue; CSV writing goes here
print(i)
if i == 4999:
return
consumer = Thread(target=consume)
consumer.setDaemon(True)
consumer.start()
def produce(i):
# Data processing goes here; row goes into queue
queue.put(i)
with ThreadPoolExecutor(max_workers=10) as executor:
for i in range(5000):
executor.submit(produce, i)
consumer.join()
这是一些代码,它还处理了令人头疼的 unicode 问题:
def ensure_bytes(s):
return s.encode('utf-8') if isinstance(s, unicode) else s
class ThreadSafeWriter(object):
'''
>>> from StringIO import StringIO
>>> f = StringIO()
>>> wtr = ThreadSafeWriter(f)
>>> wtr.writerow(['a', 'b'])
>>> f.getvalue() == "a,b\\r\\n"
True
'''
def __init__(self, *args, **kwargs):
self._writer = csv.writer(*args, **kwargs)
self._lock = threading.Lock()
def _encode(self, row):
return [ensure_bytes(cell) for cell in row]
def writerow(self, row):
row = self._encode(row)
with self._lock:
return self._writer.writerow(row)
def writerows(self, rows):
rows = (self._encode(row) for row in rows)
with self._lock:
return self._writer.writerows(rows)
# example:
with open('some.csv', 'w') as f:
writer = ThreadSafeWriter(f)
writer.write([u'中文', 'bar'])
更详细的解决方案在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.