我正在使用Django和Celery卸载长时间运行的任务。这一切都很有效,除了当任务完成时,Celery似乎将数据保存在内存中。
@shared_task
def export_xlsx():
tmpfile = settings.FILE + ".tmp"
with open(tmpfile, "w") as datafile:
datafile.write(to_xlsx())
os.rename(settings.FILE + ".tmp", settings.FILE)
return True
to_xlsx()
函数使用save_virtual_workbook
中的openpyxl.writer.excel
将Excel工作簿作为字符串返回:
def to_xlsx():
wb = Workbook()
ws = wb.active
col = 1
row = 1
articles = models.Article.objects.order_by('articleid')
for article in articles:
for articledescription in article.articledescription_set.all():
ws.cell(row=row, column=1, value=article.articleid)
ws.cell(row=row, column=3, value=articledescription.description)
row += 1
return save_virtual_workbook(wb)
似乎这个字符串永远不会从内存中释放出来:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
26536 bf 20 0 2763256 1.921g 9220 S 0.0 25.0 4:45.69 celery
26538 bf 20 0 1550080 798892 7336 R 88.0 9.9 0:08.20 celery
PID 26536已完成任务。 26538正在运行。