gpt4 book ai didi

python - 在 Telegram Bot 中发送一些照片时出现内存错误

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:45 27 4
gpt4 key购买 nike

我创建了一个 Telegram Bot ,它向用户发送一些照片。它在 4 或 5 张照片中表现良好,但之后就崩溃了。我发送这样的照片:

for i in range(len(pages)):
photo = open('pics/{0}.jpg'.format(i+1), 'rb')
bot.send_photo(chat_id=chat_id,photo=photo)

这是我的错误:

2019-03-13 14:30:52,207 (util.py:65 WorkerThread2) ERROR - TeleBot: "MemoryError occurred, args=()
Traceback (most recent call last):
File "/root/Env/pdf/lib/python3.5/site-packages/telebot/util.py", line 59, in run
task(*args, **kwargs)
File "pdf.py", line 63, in handle_docs
pages = convert_from_path('botpdf.pdf', 500)
File "/root/Env/pdf/lib/python3.5/site-packages/pdf2image/pdf2image.py", line 98, in convert_from_path
data, err = proc.communicate()
File "/usr/lib/python3.5/subprocess.py", line 1072, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.5/subprocess.py", line 1745, in _communicate
stdout = b''.join(stdout)
MemoryError
"
Traceback (most recent call last):
File "pdf.py", line 99, in <module>
bot.polling(none_stop=False, interval=0)
File "/root/Env/pdf/lib/python3.5/site-packages/telebot/__init__.py", line 389, in polling
self.__threaded_polling(none_stop, interval, timeout)
File "/root/Env/pdf/lib/python3.5/site-packages/telebot/__init__.py", line 413, in __threaded_polling
self.worker_pool.raise_exceptions()
File "/root/Env/pdf/lib/python3.5/site-packages/telebot/util.py", line 108, in raise_exceptions
six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
File "/root/Env/pdf/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/root/Env/pdf/lib/python3.5/site-packages/telebot/util.py", line 59, in run
task(*args, **kwargs)
File "pdf.py", line 63, in handle_docs
pages = convert_from_path('botpdf.pdf', 500)
File "/root/Env/pdf/lib/python3.5/site-packages/pdf2image/pdf2image.py", line 98, in convert_from_path
data, err = proc.communicate()
File "/usr/lib/python3.5/subprocess.py", line 1072, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.5/subprocess.py", line 1745, in _communicate
stdout = b''.join(stdout)
MemoryError

最佳答案

使用输出目录,您当前使用的pdf2image会加载内存中的所有图像。这会产生巨大的影响!

您编写的位置pages = Convert_from_path('botpdf.pdf', 500)

将其更改为:

import tempfile

with tempfile.TemporaryDirectory() as path:
pages = convert_from_path('botpdf.pdf', 500, output_folder=path)

这会将所有转换后的图像写入磁盘,然后再返回给您(节省大量内存)。您还可以转换为 JPEG,而不是默认的 PPM(这是一种原始格式):

pages = convert_from_path('botpdf.pdf', 500, fmt='jpeg')

但是使用此解决方案,如果 PDF 非常大,您仍然会遇到问题。

关于python - 在 Telegram Bot 中发送一些照片时出现内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55149197/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com