我创建了一个电报机器人,它向用户发送了一些照片。 在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
答案 0 :(得分:0)
使用输出目录,当前使用的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很大,您仍然会遇到问题。